|
| 1 | +import type Monaco from 'monaco-editor'; |
| 2 | +import {v4} from 'uuid'; |
| 3 | + |
| 4 | +import type {DiscardReason, PromptFile, TelemetryEvent} 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 | +} |
| 62 | + |
| 63 | +interface SendCodeAssistTelemetry { |
| 64 | + requestId: string; |
| 65 | +} |
| 66 | + |
| 67 | +interface SendCodeAssistTelemetryAccept extends SendCodeAssistTelemetry { |
| 68 | + type: 'accept'; |
| 69 | + |
| 70 | + acceptedText: string; |
| 71 | +} |
| 72 | +interface SendCodeAssistTelemetryDecline extends SendCodeAssistTelemetry { |
| 73 | + type: 'decline'; |
| 74 | + suggestionText: string; |
| 75 | + reason: DiscardReason; |
| 76 | + hitCount: number; |
| 77 | +} |
| 78 | +interface SendCodeAssistTelemetryIgnore extends SendCodeAssistTelemetry { |
| 79 | + type: 'ignore'; |
| 80 | + suggestionText: string; |
| 81 | +} |
| 82 | +type SendCodeAssistTelemetryProps = |
| 83 | + | SendCodeAssistTelemetryAccept |
| 84 | + | SendCodeAssistTelemetryDecline |
| 85 | + | SendCodeAssistTelemetryIgnore; |
| 86 | + |
| 87 | +function isAcceptType(data: SendCodeAssistTelemetryProps): data is SendCodeAssistTelemetryAccept { |
| 88 | + return data.type === 'accept'; |
| 89 | +} |
| 90 | +function isDeclineType(data: SendCodeAssistTelemetryProps): data is SendCodeAssistTelemetryDecline { |
| 91 | + return data.type === 'decline'; |
| 92 | +} |
| 93 | +function isIgnoreType(data: SendCodeAssistTelemetryProps): data is SendCodeAssistTelemetryIgnore { |
| 94 | + return data.type === 'ignore'; |
| 95 | +} |
| 96 | + |
| 97 | +export function sendCodeAssistTelemetry(props: SendCodeAssistTelemetryProps) { |
| 98 | + let data: TelemetryEvent | null = null; |
| 99 | + const timestamp = Date.now(); |
| 100 | + if (isAcceptType(props)) { |
| 101 | + const {requestId, acceptedText} = props; |
| 102 | + data = { |
| 103 | + Accepted: { |
| 104 | + RequestId: requestId, |
| 105 | + Timestamp: timestamp, |
| 106 | + AcceptedText: acceptedText, |
| 107 | + ConvertedText: acceptedText, |
| 108 | + }, |
| 109 | + }; |
| 110 | + } else if (isDeclineType(props)) { |
| 111 | + const {requestId, suggestionText, reason, hitCount} = props; |
| 112 | + data = { |
| 113 | + Discarded: { |
| 114 | + RequestId: requestId, |
| 115 | + Timestamp: timestamp, |
| 116 | + DiscardReason: reason, |
| 117 | + DiscardedText: suggestionText, |
| 118 | + CacheHitCount: hitCount, |
| 119 | + }, |
| 120 | + }; |
| 121 | + } else if (isIgnoreType(props)) { |
| 122 | + const {requestId, suggestionText} = props; |
| 123 | + data = { |
| 124 | + Ignored: { |
| 125 | + RequestId: requestId, |
| 126 | + Timestamp: timestamp, |
| 127 | + IgnoredText: suggestionText, |
| 128 | + }, |
| 129 | + }; |
| 130 | + } |
| 131 | + if (data) { |
| 132 | + window.api.codeAssistant?.sendCodeAssistTelemetry(data); |
| 133 | + } |
| 134 | +} |
0 commit comments