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