|
| 1 | +import * as monaco from 'monaco-editor'; |
| 2 | +import { sendMessage } from './chatRelay'; |
| 3 | +import useAppStore from '../store/store'; |
| 4 | +import { editorsContent } from '../types/components/AIAssistant.types'; |
| 5 | +import { getLastActivity } from './activityTracker'; |
| 6 | + |
| 7 | +let lastRequestTime: number = 0; |
| 8 | +let isProcessing: boolean = false; |
| 9 | + |
| 10 | +export const registerAutocompletion = ( |
| 11 | + language: 'concerto' | 'markdown' | 'json', |
| 12 | + monacoInstance: typeof monaco |
| 13 | +) => { |
| 14 | + try { |
| 15 | + const provider = monacoInstance.languages.registerInlineCompletionsProvider(language, { |
| 16 | + provideInlineCompletions: async (model, position, _context, token) => { |
| 17 | + if (token.isCancellationRequested) { |
| 18 | + return { items: [] }; |
| 19 | + } |
| 20 | + |
| 21 | + const { aiConfig } = useAppStore.getState(); |
| 22 | + const enableInlineSuggestions = aiConfig?.enableInlineSuggestions !== false; |
| 23 | + if (!enableInlineSuggestions) { |
| 24 | + return { items: [] }; |
| 25 | + } |
| 26 | + |
| 27 | + const initialTime = Date.now(); |
| 28 | + |
| 29 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 30 | + |
| 31 | + const lastActivity = getLastActivity(language); |
| 32 | + if (lastActivity > initialTime) { |
| 33 | + return { items: [] }; |
| 34 | + } |
| 35 | + |
| 36 | + if (token.isCancellationRequested) { |
| 37 | + return { items: [] }; |
| 38 | + } |
| 39 | + |
| 40 | + const currentTime = Date.now(); |
| 41 | + if (isProcessing || (currentTime - lastRequestTime < 2000)) { |
| 42 | + return { items: [] }; |
| 43 | + } |
| 44 | + |
| 45 | + isProcessing = true; |
| 46 | + lastRequestTime = currentTime; |
| 47 | + |
| 48 | + try { |
| 49 | + const result = await getInlineCompletions(model, position, language, monacoInstance); |
| 50 | + return result; |
| 51 | + } finally { |
| 52 | + isProcessing = false; |
| 53 | + } |
| 54 | + }, |
| 55 | + freeInlineCompletions: (_completions) => { |
| 56 | + }, |
| 57 | + }); |
| 58 | + return provider; |
| 59 | + } catch (error) { |
| 60 | + console.error('Error registering completion provider:', error); |
| 61 | + return null; |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +const getInlineCompletions = async ( |
| 66 | + model: monaco.editor.ITextModel, |
| 67 | + position: monaco.Position, |
| 68 | + language: 'concerto' | 'markdown' | 'json', |
| 69 | + monacoInstance: typeof monaco |
| 70 | +): Promise<{ items: monaco.languages.InlineCompletion[] }> => { |
| 71 | + const { aiConfig } = useAppStore.getState(); |
| 72 | + |
| 73 | + if (!aiConfig) { |
| 74 | + return { items: [] }; |
| 75 | + } |
| 76 | + |
| 77 | + const lineContent = model.getLineContent(position.lineNumber); |
| 78 | + const textBeforeCursor = lineContent.substring(0, position.column - 1); |
| 79 | + const textAfterCursor = lineContent.substring(position.column - 1); |
| 80 | + |
| 81 | + if (!textBeforeCursor.trim() || textBeforeCursor.length < 2) { |
| 82 | + return { |
| 83 | + items: [] |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + const startLine = Math.max(1, position.lineNumber - 20); |
| 88 | + const endLine = Math.min(model.getLineCount(), position.lineNumber + 20); |
| 89 | + const contextLines: string[] = []; |
| 90 | + |
| 91 | + for (let i = startLine; i <= endLine; i++) { |
| 92 | + if (i === position.lineNumber) { |
| 93 | + const fullCurrentLine = textBeforeCursor + '<CURSOR>' + textAfterCursor; |
| 94 | + contextLines.push(fullCurrentLine); |
| 95 | + } else if (i < position.lineNumber) { |
| 96 | + contextLines.push(model.getLineContent(i)); |
| 97 | + } else { |
| 98 | + contextLines.push(model.getLineContent(i)); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + const contextText = contextLines.join('\n'); |
| 103 | + |
| 104 | + const editorsContent: editorsContent = { |
| 105 | + editorTemplateMark: useAppStore.getState().editorValue, |
| 106 | + editorModelCto: useAppStore.getState().editorModelCto, |
| 107 | + editorAgreementData: useAppStore.getState().editorAgreementData, |
| 108 | + }; |
| 109 | + const prompt = `Current context:\n${contextText}`; |
| 110 | + |
| 111 | + try { |
| 112 | + let completion = ''; |
| 113 | + |
| 114 | + await sendMessage( |
| 115 | + prompt, |
| 116 | + 'inlineSuggestion', |
| 117 | + editorsContent, |
| 118 | + false, |
| 119 | + language, |
| 120 | + (chunk) => { |
| 121 | + completion += chunk; |
| 122 | + }, |
| 123 | + (error) => { |
| 124 | + console.error('Autocompletion error:', error); |
| 125 | + } |
| 126 | + ); |
| 127 | + |
| 128 | + completion = completion.trim(); |
| 129 | + |
| 130 | + completion = completion.replace(/^```[\s\S]*?\n/, '').replace(/\n```$/, ''); |
| 131 | + completion = completion.replace(/^`/, '').replace(/`$/, ''); |
| 132 | + |
| 133 | + if (!completion || completion.length < 2) { |
| 134 | + return { items: [] }; |
| 135 | + } |
| 136 | + |
| 137 | + const inlineCompletion: monaco.languages.InlineCompletion = { |
| 138 | + insertText: completion, |
| 139 | + range: new monacoInstance.Range( |
| 140 | + position.lineNumber, |
| 141 | + position.column, |
| 142 | + position.lineNumber, |
| 143 | + position.column |
| 144 | + ), |
| 145 | + filterText: textBeforeCursor, |
| 146 | + }; |
| 147 | + |
| 148 | + return { |
| 149 | + items: [inlineCompletion], |
| 150 | + }; |
| 151 | + } catch (error) { |
| 152 | + console.error('Error getting AI completion:', error); |
| 153 | + return { items: [] }; |
| 154 | + } |
| 155 | +}; |
0 commit comments