|
| 1 | +// IMPORTANT! |
| 2 | +// In the future code assist api will be provided to ydb-ui component explicitly by consumer service. |
| 3 | +// Current solution is temporary and aimed to satisfy internal puproses. |
| 4 | +// It means this whole file will be moved to customer service |
| 5 | + |
| 6 | +import type {PromptFile, Suggestions} from '@ydb-platform/monaco-ghost'; |
| 7 | + |
| 8 | +import {codeAssistBackend as CODE_ASSISTANT_BACKEND} from '../../store'; |
| 9 | +import type { |
| 10 | + CodeAssistSuggestionsFiles, |
| 11 | + CodeAssistSuggestionsResponse, |
| 12 | + TelemetryEvent, |
| 13 | + TelemetryOpenTabs, |
| 14 | +} from '../../types/api/codeAssist'; |
| 15 | + |
| 16 | +import {BaseYdbAPI} from './base'; |
| 17 | +const ideInfo = { |
| 18 | + Ide: 'ydb', |
| 19 | + IdeVersion: '1', |
| 20 | + PluginFamily: 'ydb', |
| 21 | + PluginVersion: '0.2', |
| 22 | +}; |
| 23 | + |
| 24 | +const limitForTab = 10_000; |
| 25 | +const limitBeforeCursor = 8_000; |
| 26 | +const limitAfterCursor = 1_000; |
| 27 | + |
| 28 | +function prepareCodeAssistTabs(tabs: TelemetryOpenTabs): TelemetryOpenTabs { |
| 29 | + return tabs.map((tab) => { |
| 30 | + const text = tab.Text; |
| 31 | + if (text.length > limitForTab) { |
| 32 | + return { |
| 33 | + ...tab, |
| 34 | + Text: text.slice(0, limitForTab), |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + return tab; |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +function prepareCodeAssistPrompt(promptFiles: PromptFile[]): CodeAssistSuggestionsFiles { |
| 43 | + return promptFiles.map((file) => { |
| 44 | + const cursorLine = file.cursorPosition.lineNumber; |
| 45 | + const cursorCol = file.cursorPosition.column; |
| 46 | + |
| 47 | + return { |
| 48 | + Fragments: file.fragments.map((fragment) => { |
| 49 | + let text = fragment.text; |
| 50 | + const isBeforeCursor = |
| 51 | + fragment.end.lineNumber < cursorLine || |
| 52 | + (fragment.end.lineNumber === cursorLine && fragment.end.column <= cursorCol); |
| 53 | + const isAfterCursor = |
| 54 | + fragment.start.lineNumber > cursorLine || |
| 55 | + (fragment.start.lineNumber === cursorLine && fragment.start.column > cursorCol); |
| 56 | + |
| 57 | + if (isBeforeCursor) { |
| 58 | + text = text.slice(-limitBeforeCursor); |
| 59 | + } else if (isAfterCursor) { |
| 60 | + text = text.slice(0, limitAfterCursor); |
| 61 | + } |
| 62 | + |
| 63 | + return { |
| 64 | + Text: text, |
| 65 | + Start: { |
| 66 | + Ln: fragment.start.lineNumber, |
| 67 | + Col: fragment.start.column, |
| 68 | + }, |
| 69 | + End: { |
| 70 | + Ln: fragment.end.lineNumber, |
| 71 | + Col: fragment.end.column, |
| 72 | + }, |
| 73 | + }; |
| 74 | + }), |
| 75 | + Cursor: { |
| 76 | + Ln: cursorLine, |
| 77 | + Col: cursorCol, |
| 78 | + }, |
| 79 | + Path: `${file.path}.yql`, |
| 80 | + }; |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +export class CodeAssistAPI extends BaseYdbAPI { |
| 85 | + getPath(path: string) { |
| 86 | + return `${CODE_ASSISTANT_BACKEND ?? ''}${path}`; |
| 87 | + } |
| 88 | + |
| 89 | + async getCodeAssistSuggestions(data: PromptFile[]): Promise<Suggestions> { |
| 90 | + const request: CodeAssistSuggestionsFiles = prepareCodeAssistPrompt(data); |
| 91 | + |
| 92 | + const response = await this.post<CodeAssistSuggestionsResponse>( |
| 93 | + this.getPath('/code-assist-suggestion'), |
| 94 | + { |
| 95 | + Files: request, |
| 96 | + ContextCreateType: 1, |
| 97 | + IdeInfo: ideInfo, |
| 98 | + }, |
| 99 | + null, |
| 100 | + { |
| 101 | + concurrentId: 'code-assist-suggestion', |
| 102 | + collectRequest: false, |
| 103 | + }, |
| 104 | + ); |
| 105 | + |
| 106 | + return { |
| 107 | + items: response.Suggests.map((suggestion) => suggestion.Text), |
| 108 | + requestId: response.RequestId, |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + sendCodeAssistTelemetry(data: TelemetryEvent) { |
| 113 | + return this.post('/code-assist-telemetry', data, null, { |
| 114 | + concurrentId: 'code-assist-telemetry', |
| 115 | + collectRequest: true, |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + sendCodeAssistOpenTabs(data: TelemetryOpenTabs) { |
| 120 | + return this.post( |
| 121 | + '/code-assist-telemetry', |
| 122 | + {OpenTabs: {Tabs: prepareCodeAssistTabs(data), IdeInfo: ideInfo}}, |
| 123 | + null, |
| 124 | + { |
| 125 | + concurrentId: 'code-assist-telemetry', |
| 126 | + collectRequest: false, |
| 127 | + }, |
| 128 | + ); |
| 129 | + } |
| 130 | +} |
0 commit comments