| 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 { NextEditProvider } from "../NextEditProvider"; |
| 7 | import { RecentlyEditedRange } from "../types"; |
| 8 | import { getAutocompleteContext } from "./autocompleteContextFetching"; |
| 9 | import { createDiff, DiffFormatType } from "./diffFormatting"; |
| 10 | import { |
| 11 | getPrevEditsDescending, |
| 12 | prevEdit, |
| 13 | prevEditLruCache, |
| 14 | setPrevEdit, |
| 15 | } from "./prevEditLruCache"; |
| 16 | |
| 17 | const randomNumberBetween = (min: number, max: number) => { |
| 18 | min = Math.ceil(min); // Ensure min is an integer |
| 19 | max = Math.floor(max); // Ensure max is an integer |
| 20 | return Math.floor(Math.random() * (max - min + 1)) + min; |
| 21 | }; |
| 22 | |
| 23 | interface ProcessNextEditDataParams { |
| 24 | filePath: string; |
| 25 | beforeContent: string; |
| 26 | afterContent: string; |
| 27 | cursorPosBeforeEdit: Position; |
| 28 | cursorPosAfterPrevEdit: Position; |
| 29 | ide: IDE; |
| 30 | configHandler: ConfigHandler; |
| 31 | getDefinitionsFromLsp: GetLspDefinitionsFunction; |
| 32 | recentlyEditedRanges: RecentlyEditedRange[]; |
| 33 | recentlyVisitedRanges: AutocompleteCodeSnippet[]; |
| 34 | workspaceDir: string; |
| 35 | modelNameOrInstance?: string | undefined; |
| 36 | } |
| 37 | |
| 38 | interface filenameAndDiff { |
| 39 | filename: string; |
| 40 | diff: string; |
| 41 | } |
| 42 | |
| 43 | export const processNextEditData = async ({ |
| 44 | filePath, |
| 45 | beforeContent, |
| 46 | afterContent, |
| 47 | cursorPosBeforeEdit, |
| 48 | cursorPosAfterPrevEdit, |
| 49 | ide, |
| 50 | configHandler, |
| 51 | getDefinitionsFromLsp, |
| 52 | recentlyEditedRanges, |
| 53 | recentlyVisitedRanges, |
| 54 | workspaceDir, |
| 55 | modelNameOrInstance, |
| 56 | }: ProcessNextEditDataParams) => { |
| 57 | // To switch to the user's autocomplete model, uncomment the following lines |
| 58 | // const { config } = await configHandler.loadConfig(); |
| 59 | // const autocompleteModel = |
| 60 | // (modelNameOrInstance || config?.selectedModelByRole.autocomplete) ?? |
| 61 | // undefined; |
| 62 | |
| 63 | const modelName = "Codestral"; |
| 64 | const modelProvider = "mistral"; |
| 65 | const maxPromptTokens = randomNumberBetween(500, 12000); |
| 66 | |
| 67 | const autocompleteContext = await getAutocompleteContext( |
| 68 | filePath, |
| 69 | cursorPosBeforeEdit, |
| 70 | ide, |
| 71 | configHandler, |
| 72 | getDefinitionsFromLsp, |
| 73 | recentlyEditedRanges, |
| 74 | recentlyVisitedRanges, |
| 75 | maxPromptTokens, |
| 76 | beforeContent, |
| 77 | modelName, |
| 78 | ); |
| 79 | |
| 80 | NextEditProvider.getInstance().addAutocompleteContext(autocompleteContext); |
| 81 | |
| 82 | // console.log( |
| 83 | // createDiff(beforeContent, afterContent, filePath, DiffFormatType.Unified), |
| 84 | // ); |
| 85 | |
| 86 | let filenamesAndDiffs: filenameAndDiff[] = []; |
| 87 | |
| 88 | const timestamp = Date.now(); |
| 89 | let prevEdits: prevEdit[] = getPrevEditsDescending(); // edits from most to least recent |
| 90 | if (prevEdits.length > 0) { |
| 91 | // if last edit was 10+ minutes ago or the workspace changed, forget previous edits |
| 92 | if ( |
| 93 | timestamp - prevEdits[0].timestamp >= 1000 * 60 * 10 || |
| 94 | workspaceDir !== prevEdits[0].workspaceUri |
| 95 | ) { |
| 96 | prevEditLruCache.clear(); |
| 97 | prevEdits = []; |
| 98 | } |
| 99 | |
| 100 | // extract filenames and diffs for logging |
| 101 | filenamesAndDiffs = prevEdits.map( |
| 102 | (edit) => |
| 103 | ({ |
| 104 | // filename relative to workspace dir |
| 105 | filename: edit.fileUri |
| 106 | .replace(edit.workspaceUri, "") |
| 107 | .replace(/^[/\\]/, ""), |
| 108 | |
| 109 | // diff without the first 4 lines (the file header) |
| 110 | diff: edit.unidiff.split("\n").slice(4).join("\n"), |
| 111 | }) as filenameAndDiff, |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | if (filenamesAndDiffs.length > 0) { |
| 116 | // if there are previous edits, log |
| 117 | void DataLogger.getInstance().logDevData({ |
| 118 | name: "nextEditWithHistory", |
| 119 | data: { |
| 120 | previousEdits: filenamesAndDiffs, |
| 121 | fileURI: filePath, |
| 122 | workspaceDirURI: workspaceDir, |
| 123 | beforeContent, |
| 124 | afterContent, |
| 125 | beforeCursorPos: cursorPosBeforeEdit, |
| 126 | afterCursorPos: cursorPosAfterPrevEdit, |
| 127 | context: autocompleteContext, |
| 128 | modelProvider, |
| 129 | modelName, |
| 130 | modelTitle: modelName, |
| 131 | }, |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | // add current edit to history |
| 136 | const thisEdit: prevEdit = { |
| 137 | unidiff: createDiff({ |
| 138 | beforeContent: beforeContent, |
| 139 | afterContent: afterContent, |
| 140 | filePath: filePath, |
| 141 | diffType: DiffFormatType.Unified, |
| 142 | contextLines: 25, // storing many context lines for downstream trimming |
| 143 | }), |
| 144 | fileUri: filePath, |
| 145 | workspaceUri: workspaceDir, |
| 146 | timestamp: timestamp, |
| 147 | }; |
| 148 | |
| 149 | setPrevEdit(thisEdit); |
| 150 | }; |