| 1 | import { IDE, Position } from "../.."; |
| 2 | import { AutocompleteCodeSnippet } from "../../autocomplete/snippets/types"; |
| 3 | import { GetLspDefinitionsFunction } from "../../autocomplete/types"; |
| 4 | import { ConfigHandler } from "../../config/ConfigHandler"; |
| 5 | import { DataLogger } from "../../data/log"; |
| 6 | import { RecentlyEditedRange } from "../types"; |
| 7 | import { getAutocompleteContext } from "./autocompleteContextFetching"; |
| 8 | |
| 9 | const randomNumberBetween = (min: number, max: number) => { |
| 10 | min = Math.ceil(min); // Ensure min is an integer |
| 11 | max = Math.floor(max); // Ensure max is an integer |
| 12 | return Math.floor(Math.random() * (max - min + 1)) + min; |
| 13 | }; |
| 14 | |
| 15 | interface ProcessNextEditDataParams { |
| 16 | filePath: string; |
| 17 | beforeContent: string; |
| 18 | afterContent: string; |
| 19 | cursorPosBeforeEdit: Position; |
| 20 | cursorPosAfterPrevEdit: Position; |
| 21 | ide: IDE; |
| 22 | configHandler: ConfigHandler; |
| 23 | getDefinitionsFromLsp: GetLspDefinitionsFunction; |
| 24 | recentlyEditedRanges: RecentlyEditedRange[]; |
| 25 | recentlyVisitedRanges: AutocompleteCodeSnippet[]; |
| 26 | workspaceDir: string; |
| 27 | modelNameOrInstance?: string | undefined; |
| 28 | } |
| 29 | |
| 30 | export const processNextEditData = async ({ |
| 31 | filePath, |
| 32 | beforeContent, |
| 33 | afterContent, |
| 34 | cursorPosBeforeEdit, |
| 35 | cursorPosAfterPrevEdit, |
| 36 | ide, |
| 37 | configHandler, |
| 38 | getDefinitionsFromLsp, |
| 39 | recentlyEditedRanges, |
| 40 | recentlyVisitedRanges, |
| 41 | workspaceDir, |
| 42 | modelNameOrInstance, |
| 43 | }: ProcessNextEditDataParams) => { |
| 44 | // To switch to the user's autocomplete model, uncomment the following lines |
| 45 | // const { config } = await configHandler.loadConfig(); |
| 46 | // const autocompleteModel = |
| 47 | // (modelNameOrInstance || config?.selectedModelByRole.autocomplete) ?? |
| 48 | // undefined; |
| 49 | |
| 50 | const modelName = "Codestral"; |
| 51 | |
| 52 | const maxPromptTokens = randomNumberBetween(500, 12000); |
| 53 | |
| 54 | const autocompleteContext = await getAutocompleteContext( |
| 55 | filePath, |
| 56 | cursorPosBeforeEdit, |
| 57 | ide, |
| 58 | configHandler, |
| 59 | getDefinitionsFromLsp, |
| 60 | recentlyEditedRanges, |
| 61 | recentlyVisitedRanges, |
| 62 | maxPromptTokens, |
| 63 | beforeContent, |
| 64 | modelName, |
| 65 | ); |
| 66 | |
| 67 | // console.log( |
| 68 | // createDiff(beforeContent, afterContent, filePath, DiffFormatType.Unified), |
| 69 | // ); |
| 70 | |
| 71 | void DataLogger.getInstance().logDevData({ |
| 72 | name: "nextEdit", |
| 73 | data: { |
| 74 | fileURI: filePath, |
| 75 | workspaceDirURI: workspaceDir, |
| 76 | beforeContent, |
| 77 | afterContent, |
| 78 | beforeCursorPos: cursorPosBeforeEdit, |
| 79 | afterCursorPos: cursorPosAfterPrevEdit, |
| 80 | context: autocompleteContext, |
| 81 | }, |
| 82 | }); |
| 83 | }; |