mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
feat: add configuration file detection and sensitive content checks in code completion
This commit is contained in:
parent
d4a4ed1e1c
commit
a88f8646e6
1 changed files with 62 additions and 3 deletions
|
@ -12,12 +12,39 @@ function debug(...args: any[]) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Config file patterns and extensions
|
||||||
|
const CONFIG_FILE_EXTENSIONS = ['.conf', '.config']
|
||||||
|
const SENSITIVE_CONTENT_PATTERNS = [
|
||||||
|
/-----BEGIN [A-Z ]+ PRIVATE KEY-----/,
|
||||||
|
/-----BEGIN CERTIFICATE-----/,
|
||||||
|
/apiKey\s*[:=]\s*["'][a-zA-Z0-9]+["']/,
|
||||||
|
/password\s*[:=]\s*["'][^"']+["']/,
|
||||||
|
/secret\s*[:=]\s*["'][^"']+["']/,
|
||||||
|
]
|
||||||
|
|
||||||
function useCodeCompletion() {
|
function useCodeCompletion() {
|
||||||
const editorRef = ref<Editor>()
|
const editorRef = ref<Editor>()
|
||||||
const currentGhostText = ref<string>('')
|
const currentGhostText = ref<string>('')
|
||||||
|
const isConfigFile = ref<boolean>(false)
|
||||||
|
|
||||||
const ws = openai.code_completion()
|
const ws = openai.code_completion()
|
||||||
|
|
||||||
|
// Check if the current file is a configuration file
|
||||||
|
function checkIfConfigFile(filename: string, content: string): boolean {
|
||||||
|
// Check file extension
|
||||||
|
const hasConfigExtension = CONFIG_FILE_EXTENSIONS.some(ext => filename.toLowerCase().endsWith(ext))
|
||||||
|
|
||||||
|
// Check if it's an Nginx configuration file based on common patterns
|
||||||
|
const hasNginxPatterns = /server\s*\{|location\s*\/|http\s*\{|upstream\s*[\w-]+\s*\{/.test(content)
|
||||||
|
|
||||||
|
return hasConfigExtension || hasNginxPatterns
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if content contains sensitive information that shouldn't be sent
|
||||||
|
function containsSensitiveContent(content: string): boolean {
|
||||||
|
return SENSITIVE_CONTENT_PATTERNS.some(pattern => pattern.test(content))
|
||||||
|
}
|
||||||
|
|
||||||
function getAISuggestions(code: string, context: string, position: Point, callback: (suggestion: string) => void, language: string = 'nginx', suffix: string = '', requestId: string) {
|
function getAISuggestions(code: string, context: string, position: Point, callback: (suggestion: string) => void, language: string = 'nginx', suffix: string = '', requestId: string) {
|
||||||
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
||||||
debug('WebSocket is not open')
|
debug('WebSocket is not open')
|
||||||
|
@ -29,6 +56,17 @@ function useCodeCompletion() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skip if not a config file or contains sensitive content
|
||||||
|
if (!isConfigFile.value) {
|
||||||
|
debug('Skipping AI suggestions for non-config file')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (containsSensitiveContent(context)) {
|
||||||
|
debug('Skipping AI suggestions due to sensitive content')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const message = {
|
const message = {
|
||||||
context,
|
context,
|
||||||
code,
|
code,
|
||||||
|
@ -57,8 +95,20 @@ function useCodeCompletion() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isConfigFile.value) {
|
||||||
|
debug('Skipping ghost text for non-config file')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const currentText = editorRef.value.getValue()
|
const currentText = editorRef.value.getValue()
|
||||||
|
|
||||||
|
// Skip if content contains sensitive information
|
||||||
|
if (containsSensitiveContent(currentText)) {
|
||||||
|
debug('Skipping ghost text due to sensitive content')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const cursorPosition = editorRef.value.getCursorPosition()
|
const cursorPosition = editorRef.value.getCursorPosition()
|
||||||
|
|
||||||
// Get all text before the current cursor position as the code part for the request
|
// Get all text before the current cursor position as the code part for the request
|
||||||
|
@ -175,7 +225,7 @@ function useCodeCompletion() {
|
||||||
|
|
||||||
debug('Editor initialized')
|
debug('Editor initialized')
|
||||||
|
|
||||||
async function init(editor: Editor) {
|
async function init(editor: Editor, filename: string = '') {
|
||||||
const { enabled } = await openai.get_code_completion_enabled_status()
|
const { enabled } = await openai.get_code_completion_enabled_status()
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
debug('Code completion is not enabled')
|
debug('Code completion is not enabled')
|
||||||
|
@ -184,6 +234,11 @@ function useCodeCompletion() {
|
||||||
|
|
||||||
editorRef.value = editor
|
editorRef.value = editor
|
||||||
|
|
||||||
|
// Determine if the current file is a configuration file
|
||||||
|
const content = editor.getValue()
|
||||||
|
isConfigFile.value = checkIfConfigFile(filename, content)
|
||||||
|
debug(`File type check: isConfigFile=${isConfigFile.value}, filename=${filename}`)
|
||||||
|
|
||||||
// Set up Tab key handler
|
// Set up Tab key handler
|
||||||
setupTabHandler(editor)
|
setupTabHandler(editor)
|
||||||
|
|
||||||
|
@ -195,7 +250,9 @@ function useCodeCompletion() {
|
||||||
|
|
||||||
if (e.action === 'insert' || e.action === 'remove') {
|
if (e.action === 'insert' || e.action === 'remove') {
|
||||||
// Clear current ghost text
|
// Clear current ghost text
|
||||||
debouncedApplyGhostText()
|
if (isConfigFile.value) {
|
||||||
|
debouncedApplyGhostText()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -203,7 +260,9 @@ function useCodeCompletion() {
|
||||||
editor.selection.on('changeCursor', () => {
|
editor.selection.on('changeCursor', () => {
|
||||||
debug('Cursor changed')
|
debug('Cursor changed')
|
||||||
clearGhostText()
|
clearGhostText()
|
||||||
debouncedApplyGhostText()
|
if (isConfigFile.value) {
|
||||||
|
debouncedApplyGhostText()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}, 2000)
|
}, 2000)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue