@@ -12,12 +12,39 @@ function debug(...args: any[]) {
12
12
}
13
13
}
14
14
15
+ // Config file patterns and extensions
16
+ const CONFIG_FILE_EXTENSIONS = [ '.conf' , '.config' ]
17
+ const SENSITIVE_CONTENT_PATTERNS = [
18
+ / - - - - - B E G I N [ A - Z ] + P R I V A T E K E Y - - - - - / ,
19
+ / - - - - - B E G I N C E R T I F I C A T E - - - - - / ,
20
+ / a p i K e y \s * [: = ] \s * [ " ' ] [ a - z A - Z 0 - 9 ] + [ " ' ] / ,
21
+ / p a s s w o r d \s * [: = ] \s * [ " ' ] [ ^ " ' ] + [ " ' ] / ,
22
+ / s e c r e t \s * [: = ] \s * [ " ' ] [ ^ " ' ] + [ " ' ] / ,
23
+ ]
24
+
15
25
function useCodeCompletion ( ) {
16
26
const editorRef = ref < Editor > ( )
17
27
const currentGhostText = ref < string > ( '' )
28
+ const isConfigFile = ref < boolean > ( false )
18
29
19
30
const ws = openai . code_completion ( )
20
31
32
+ // Check if the current file is a configuration file
33
+ function checkIfConfigFile ( filename : string , content : string ) : boolean {
34
+ // Check file extension
35
+ const hasConfigExtension = CONFIG_FILE_EXTENSIONS . some ( ext => filename . toLowerCase ( ) . endsWith ( ext ) )
36
+
37
+ // Check if it's an Nginx configuration file based on common patterns
38
+ const hasNginxPatterns = / s e r v e r \s * \{ | l o c a t i o n \s * \/ | h t t p \s * \{ | u p s t r e a m \s * [ \w - ] + \s * \{ / . test ( content )
39
+
40
+ return hasConfigExtension || hasNginxPatterns
41
+ }
42
+
43
+ // Check if content contains sensitive information that shouldn't be sent
44
+ function containsSensitiveContent ( content : string ) : boolean {
45
+ return SENSITIVE_CONTENT_PATTERNS . some ( pattern => pattern . test ( content ) )
46
+ }
47
+
21
48
function getAISuggestions ( code : string , context : string , position : Point , callback : ( suggestion : string ) => void , language : string = 'nginx' , suffix : string = '' , requestId : string ) {
22
49
if ( ! ws || ws . readyState !== WebSocket . OPEN ) {
23
50
debug ( 'WebSocket is not open' )
@@ -29,6 +56,17 @@ function useCodeCompletion() {
29
56
return
30
57
}
31
58
59
+ // Skip if not a config file or contains sensitive content
60
+ if ( ! isConfigFile . value ) {
61
+ debug ( 'Skipping AI suggestions for non-config file' )
62
+ return
63
+ }
64
+
65
+ if ( containsSensitiveContent ( context ) ) {
66
+ debug ( 'Skipping AI suggestions due to sensitive content' )
67
+ return
68
+ }
69
+
32
70
const message = {
33
71
context,
34
72
code,
@@ -57,8 +95,20 @@ function useCodeCompletion() {
57
95
return
58
96
}
59
97
98
+ if ( ! isConfigFile . value ) {
99
+ debug ( 'Skipping ghost text for non-config file' )
100
+ return
101
+ }
102
+
60
103
try {
61
104
const currentText = editorRef . value . getValue ( )
105
+
106
+ // Skip if content contains sensitive information
107
+ if ( containsSensitiveContent ( currentText ) ) {
108
+ debug ( 'Skipping ghost text due to sensitive content' )
109
+ return
110
+ }
111
+
62
112
const cursorPosition = editorRef . value . getCursorPosition ( )
63
113
64
114
// Get all text before the current cursor position as the code part for the request
@@ -175,7 +225,7 @@ function useCodeCompletion() {
175
225
176
226
debug ( 'Editor initialized' )
177
227
178
- async function init ( editor : Editor ) {
228
+ async function init ( editor : Editor , filename : string = '' ) {
179
229
const { enabled } = await openai . get_code_completion_enabled_status ( )
180
230
if ( ! enabled ) {
181
231
debug ( 'Code completion is not enabled' )
@@ -184,6 +234,11 @@ function useCodeCompletion() {
184
234
185
235
editorRef . value = editor
186
236
237
+ // Determine if the current file is a configuration file
238
+ const content = editor . getValue ( )
239
+ isConfigFile . value = checkIfConfigFile ( filename , content )
240
+ debug ( `File type check: isConfigFile=${ isConfigFile . value } , filename=${ filename } ` )
241
+
187
242
// Set up Tab key handler
188
243
setupTabHandler ( editor )
189
244
@@ -195,15 +250,19 @@ function useCodeCompletion() {
195
250
196
251
if ( e . action === 'insert' || e . action === 'remove' ) {
197
252
// Clear current ghost text
198
- debouncedApplyGhostText ( )
253
+ if ( isConfigFile . value ) {
254
+ debouncedApplyGhostText ( )
255
+ }
199
256
}
200
257
} )
201
258
202
259
// Listen for cursor changes, using debounce
203
260
editor . selection . on ( 'changeCursor' , ( ) => {
204
261
debug ( 'Cursor changed' )
205
262
clearGhostText ( )
206
- debouncedApplyGhostText ( )
263
+ if ( isConfigFile . value ) {
264
+ debouncedApplyGhostText ( )
265
+ }
207
266
} )
208
267
} , 2000 )
209
268
}
0 commit comments