|
| 1 | +import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'; |
| 2 | + |
| 3 | +import type {DiscardReason} from '../../types/api/codeAssistant'; |
| 4 | +import {getPromptFileContent} from '../../utils/monaco/codeAssistTelemetry'; |
| 5 | + |
| 6 | +import type { |
| 7 | + EnrichedCompletion, |
| 8 | + ICodeCompletionAPI, |
| 9 | + ICodeCompletionService, |
| 10 | + ITelemetryService, |
| 11 | + InternalSuggestion, |
| 12 | +} from './types'; |
| 13 | + |
| 14 | +export class CodeCompletionService implements ICodeCompletionService { |
| 15 | + private prevSuggestions: InternalSuggestion[] = []; |
| 16 | + private timer: number | null = null; |
| 17 | + private readonly api: ICodeCompletionAPI; |
| 18 | + private readonly telemetry: ITelemetryService; |
| 19 | + private readonly editor?: monaco.editor.IStandaloneCodeEditor; |
| 20 | + |
| 21 | + constructor( |
| 22 | + api: ICodeCompletionAPI, |
| 23 | + telemetry: ITelemetryService, |
| 24 | + editor?: monaco.editor.IStandaloneCodeEditor, |
| 25 | + ) { |
| 26 | + this.api = api; |
| 27 | + this.telemetry = telemetry; |
| 28 | + this.editor = editor; |
| 29 | + } |
| 30 | + |
| 31 | + handleItemDidShow( |
| 32 | + _completions: monaco.languages.InlineCompletions<EnrichedCompletion>, |
| 33 | + item: EnrichedCompletion, |
| 34 | + ) { |
| 35 | + for (const suggests of this.prevSuggestions) { |
| 36 | + for (const completion of suggests.items) { |
| 37 | + if (completion.pristine === item.pristine) { |
| 38 | + suggests.shownCount++; |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + async provideInlineCompletions( |
| 46 | + model: monaco.editor.ITextModel, |
| 47 | + position: monaco.Position, |
| 48 | + _context: monaco.languages.InlineCompletionContext, |
| 49 | + _token: monaco.CancellationToken, |
| 50 | + ) { |
| 51 | + const cachedCompletions = this.getCachedCompletion(model, position); |
| 52 | + if (cachedCompletions.length) { |
| 53 | + return {items: cachedCompletions}; |
| 54 | + } |
| 55 | + while (this.prevSuggestions.length > 0) { |
| 56 | + this.dismissCompletion(this.prevSuggestions.pop()); |
| 57 | + } |
| 58 | + const {suggestions, requestId} = await this.getSuggestions(model, position); |
| 59 | + |
| 60 | + this.prevSuggestions = [{items: suggestions, shownCount: 0, requestId}]; |
| 61 | + return { |
| 62 | + items: suggestions, |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + handlePartialAccept( |
| 67 | + _completions: monaco.languages.InlineCompletions, |
| 68 | + item: monaco.languages.InlineCompletion, |
| 69 | + acceptedLetters: number, |
| 70 | + ) { |
| 71 | + const {command} = item; |
| 72 | + const commandArguments = command?.arguments?.[0] ?? {}; |
| 73 | + const {suggestionText, requestId, prevWordLength = 0} = commandArguments; |
| 74 | + const cachedSuggestions = this.prevSuggestions.find((el) => { |
| 75 | + return el.items.some((item) => item.pristine === suggestionText); |
| 76 | + }); |
| 77 | + if (requestId && suggestionText && typeof item.insertText === 'string') { |
| 78 | + const acceptedText = item.insertText.slice(prevWordLength, acceptedLetters); |
| 79 | + if (acceptedText) { |
| 80 | + if (cachedSuggestions) { |
| 81 | + cachedSuggestions.wasAccepted = true; |
| 82 | + } |
| 83 | + this.telemetry.sendAcceptTelemetry(requestId, acceptedText); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + handleAccept({requestId, suggestionText}: {requestId: string; suggestionText: string}) { |
| 89 | + this.emptyCache(); |
| 90 | + this.telemetry.sendAcceptTelemetry(requestId, suggestionText); |
| 91 | + } |
| 92 | + |
| 93 | + commandDiscard(reason: DiscardReason = 'OnCancel'): void { |
| 94 | + while (this.prevSuggestions.length > 0) { |
| 95 | + this.discardCompletion(reason, this.prevSuggestions.pop()); |
| 96 | + } |
| 97 | + if (this.editor) { |
| 98 | + this.editor.trigger(undefined, 'editor.action.inlineSuggest.hide', undefined); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + emptyCache() { |
| 103 | + this.prevSuggestions = []; |
| 104 | + } |
| 105 | + |
| 106 | + freeInlineCompletions(): void { |
| 107 | + // This method is required by Monaco's InlineCompletionsProvider interface |
| 108 | + // but we don't need to do anything here since we handle cleanup in other methods |
| 109 | + } |
| 110 | + |
| 111 | + private getCachedCompletion( |
| 112 | + model: monaco.editor.ITextModel, |
| 113 | + position: monaco.Position, |
| 114 | + ): EnrichedCompletion[] { |
| 115 | + const completions: EnrichedCompletion[] = []; |
| 116 | + for (const suggests of this.prevSuggestions) { |
| 117 | + for (const completion of suggests.items) { |
| 118 | + if (!completion.range) { |
| 119 | + continue; |
| 120 | + } |
| 121 | + if ( |
| 122 | + position.lineNumber < completion.range.startLineNumber || |
| 123 | + position.column < completion.range.startColumn |
| 124 | + ) { |
| 125 | + continue; |
| 126 | + } |
| 127 | + const startCompletionPosition = new monaco.Position( |
| 128 | + completion.range.startLineNumber, |
| 129 | + completion.range.startColumn, |
| 130 | + ); |
| 131 | + const startOffset = model.getOffsetAt(startCompletionPosition); |
| 132 | + const endOffset = startOffset + completion.insertText.toString().length; |
| 133 | + const positionOffset = model.getOffsetAt(position); |
| 134 | + if (positionOffset > endOffset) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + const completionReplaceText = completion.insertText |
| 139 | + .toString() |
| 140 | + .slice(0, positionOffset - startOffset); |
| 141 | + |
| 142 | + const newRange = new monaco.Range( |
| 143 | + completion.range.startLineNumber, |
| 144 | + completion.range.startColumn, |
| 145 | + position.lineNumber, |
| 146 | + position.column, |
| 147 | + ); |
| 148 | + const currentReplaceText = model.getValueInRange(newRange); |
| 149 | + if (completionReplaceText.toLowerCase() === currentReplaceText.toLowerCase()) { |
| 150 | + completions.push({ |
| 151 | + insertText: |
| 152 | + currentReplaceText + |
| 153 | + completion.insertText.toString().slice(positionOffset - startOffset), |
| 154 | + range: newRange, |
| 155 | + command: completion.command, |
| 156 | + pristine: completion.pristine, |
| 157 | + }); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + return completions; |
| 162 | + } |
| 163 | + |
| 164 | + private async getSuggestions(model: monaco.editor.ITextModel, position: monaco.Position) { |
| 165 | + if (this.timer) { |
| 166 | + window.clearTimeout(this.timer); |
| 167 | + } |
| 168 | + await new Promise((r) => { |
| 169 | + this.timer = window.setTimeout(r, 200); |
| 170 | + }); |
| 171 | + let suggestions: EnrichedCompletion[] = []; |
| 172 | + let requestId = ''; |
| 173 | + try { |
| 174 | + const data = getPromptFileContent(model, position); |
| 175 | + if (!data) { |
| 176 | + return {suggestions: []}; |
| 177 | + } |
| 178 | + |
| 179 | + const codeAssistSuggestions = await this.api.getCodeAssistSuggestions(data); |
| 180 | + requestId = codeAssistSuggestions.RequestId; |
| 181 | + const {word, startColumn: lastWordStartColumn} = model.getWordUntilPosition(position); |
| 182 | + suggestions = codeAssistSuggestions.Suggests.map((el) => { |
| 183 | + const suggestionText = el.Text; |
| 184 | + const label = word + suggestionText; |
| 185 | + return { |
| 186 | + label: label, |
| 187 | + sortText: 'a', |
| 188 | + insertText: label, |
| 189 | + pristine: suggestionText, |
| 190 | + range: new monaco.Range( |
| 191 | + position.lineNumber, |
| 192 | + lastWordStartColumn, |
| 193 | + position.lineNumber, |
| 194 | + position.column, |
| 195 | + ), |
| 196 | + command: { |
| 197 | + id: 'acceptCodeAssistCompletion', |
| 198 | + title: '', |
| 199 | + arguments: [ |
| 200 | + { |
| 201 | + requestId, |
| 202 | + suggestionText: suggestionText, |
| 203 | + prevWordLength: word.length, |
| 204 | + }, |
| 205 | + ], |
| 206 | + }, |
| 207 | + }; |
| 208 | + }); |
| 209 | + } catch (err) {} |
| 210 | + return {suggestions, requestId}; |
| 211 | + } |
| 212 | + |
| 213 | + private discardCompletion(reason: DiscardReason, completion?: InternalSuggestion): void { |
| 214 | + if (completion === undefined) { |
| 215 | + return; |
| 216 | + } |
| 217 | + const {requestId, items, shownCount} = completion; |
| 218 | + if (!requestId || !items.length) { |
| 219 | + return; |
| 220 | + } |
| 221 | + for (const item of items) { |
| 222 | + this.telemetry.sendDeclineTelemetry(requestId, item.pristine, reason, shownCount); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + private dismissCompletion(completion?: InternalSuggestion): void { |
| 227 | + if (completion === undefined) { |
| 228 | + return; |
| 229 | + } |
| 230 | + const {requestId, items, shownCount, wasAccepted} = completion; |
| 231 | + |
| 232 | + if (!requestId || !items.length || !shownCount || wasAccepted) { |
| 233 | + return; |
| 234 | + } |
| 235 | + for (const item of items) { |
| 236 | + this.telemetry.sendIgnoreTelemetry(requestId, item.pristine); |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments