| 1 | /** |
| 2 | * I'm disabling this rule for the entire file under the assumption |
| 3 | * that this is a one-time migration script. I'm expecting this |
| 4 | * code to be removed in the future. |
| 5 | */ |
| 6 | /* eslint-disable max-statements */ |
| 7 | |
| 8 | import { IDE } from ".."; |
| 9 | import { deduplicateArray } from "../util"; |
| 10 | import { GlobalContext } from "../util/GlobalContext"; |
| 11 | import { resolveSerializedConfig } from "./load"; |
| 12 | import { SharedConfigSchema } from "./sharedConfig"; |
| 13 | |
| 14 | /* |
| 15 | This migration function eliminates deprecated values from the json file |
| 16 | And writes them to the shared config |
| 17 | */ |
| 18 | export function migrateJsonSharedConfig(filepath: string, ide: IDE): void { |
| 19 | const globalContext = new GlobalContext(); |
| 20 | const currentSharedConfig = globalContext.getSharedConfig(); // for merging security concerns |
| 21 | |
| 22 | try { |
| 23 | let config = resolveSerializedConfig(filepath); |
| 24 | const shareConfigUpdates: SharedConfigSchema = {}; |
| 25 | |
| 26 | let effected = false; |
| 27 | |
| 28 | const { allowAnonymousTelemetry, ...withoutAllowTelemetry } = config; |
| 29 | if (allowAnonymousTelemetry !== undefined) { |
| 30 | if (currentSharedConfig.allowAnonymousTelemetry !== false) { |
| 31 | // safe merge for security |
| 32 | shareConfigUpdates.allowAnonymousTelemetry = allowAnonymousTelemetry; |
| 33 | } |
| 34 | config = withoutAllowTelemetry; |
| 35 | effected = true; |
| 36 | } |
| 37 | |
| 38 | const { disableIndexing, ...withoutDisableIndexing } = config; |
| 39 | if (disableIndexing !== undefined) { |
| 40 | if (currentSharedConfig.disableIndexing !== true) { |
| 41 | // safe merge for security |
| 42 | shareConfigUpdates.disableIndexing = disableIndexing; |
| 43 | } |
| 44 | config = withoutDisableIndexing; |
| 45 | effected = true; |
| 46 | } |
| 47 | |
| 48 | const { disableSessionTitles, ...withoutDisableSessionTitles } = config; |
| 49 | if (config.disableSessionTitles !== undefined) { |
| 50 | if (currentSharedConfig.disableSessionTitles !== true) { |
| 51 | // safe merge for security |
| 52 | shareConfigUpdates.disableSessionTitles = config.disableSessionTitles; |
| 53 | } |
| 54 | config = withoutDisableSessionTitles; |
| 55 | effected = true; |
| 56 | } |
| 57 | |
| 58 | const { tabAutocompleteOptions, ...withoutAutocompleteOptions } = config; |
| 59 | if (tabAutocompleteOptions !== undefined) { |
| 60 | let migratedAutocomplete = { ...tabAutocompleteOptions }; |
| 61 | |
| 62 | const { useCache, ...withoutUseCache } = migratedAutocomplete; |
| 63 | if (useCache !== undefined) { |
| 64 | shareConfigUpdates.useAutocompleteCache = useCache; |
| 65 | migratedAutocomplete = withoutUseCache; |
| 66 | effected = true; |
| 67 | } |
| 68 | |
| 69 | const { multilineCompletions, ...withoutMultiline } = |
| 70 | migratedAutocomplete; |
| 71 | if (multilineCompletions !== undefined) { |
| 72 | shareConfigUpdates.useAutocompleteMultilineCompletions = |
| 73 | multilineCompletions; |
| 74 | migratedAutocomplete = withoutMultiline; |
| 75 | effected = true; |
| 76 | } |
| 77 | |
| 78 | const { disableInFiles, ...withoutDisableInFiles } = migratedAutocomplete; |
| 79 | if (disableInFiles !== undefined) { |
| 80 | if (currentSharedConfig.disableAutocompleteInFiles !== undefined) { |
| 81 | // safe merge for security |
| 82 | shareConfigUpdates.disableAutocompleteInFiles = deduplicateArray( |
| 83 | [ |
| 84 | ...currentSharedConfig.disableAutocompleteInFiles, |
| 85 | ...disableInFiles, |
| 86 | ], |
| 87 | (a, b) => a === b, |
| 88 | ); |
| 89 | } else { |
| 90 | shareConfigUpdates.disableAutocompleteInFiles = disableInFiles; |
| 91 | } |
| 92 | shareConfigUpdates.disableAutocompleteInFiles = disableInFiles; |
| 93 | migratedAutocomplete = withoutDisableInFiles; |
| 94 | effected = true; |
| 95 | } |
| 96 | |
| 97 | if (Object.keys(migratedAutocomplete).length > 0) { |
| 98 | config = { |
| 99 | ...withoutAutocompleteOptions, |
| 100 | tabAutocompleteOptions: migratedAutocomplete, |
| 101 | }; |
| 102 | } else { |
| 103 | config = withoutAutocompleteOptions; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | const { experimental, ...withoutExperimental } = config; |
| 108 | if (experimental !== undefined) { |
| 109 | let migratedExperimental = { ...experimental }; |
| 110 | |
| 111 | const { useChromiumForDocsCrawling, ...rest10 } = migratedExperimental; |
| 112 | if (useChromiumForDocsCrawling !== undefined) { |
| 113 | shareConfigUpdates.useChromiumForDocsCrawling = |
| 114 | useChromiumForDocsCrawling; |
| 115 | migratedExperimental = rest10; |
| 116 | effected = true; |
| 117 | } |
| 118 | |
| 119 | const { promptPath, ...withoutPromptPath } = migratedExperimental; |
| 120 | if (promptPath !== undefined) { |
| 121 | shareConfigUpdates.promptPath = promptPath; |
| 122 | migratedExperimental = withoutPromptPath; |
| 123 | effected = true; |
| 124 | } |
| 125 | |
| 126 | const { readResponseTTS, ...withoutReadTTS } = migratedExperimental; |
| 127 | if (readResponseTTS !== undefined) { |
| 128 | shareConfigUpdates.readResponseTTS = readResponseTTS; |
| 129 | migratedExperimental = withoutReadTTS; |
| 130 | effected = true; |
| 131 | } |
| 132 | |
| 133 | if (Object.keys(migratedExperimental).length > 0) { |
| 134 | config = { |
| 135 | ...withoutExperimental, |
| 136 | experimental: migratedExperimental, |
| 137 | }; |
| 138 | } else { |
| 139 | config = withoutExperimental; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | const { ui, ...withoutUI } = config; |
| 144 | if (ui !== undefined) { |
| 145 | let migratedUI = { ...ui }; |
| 146 | |
| 147 | const { codeBlockToolbarPosition, ...withoutToolbarPosition } = |
| 148 | migratedUI; |
| 149 | if (codeBlockToolbarPosition !== undefined) { |
| 150 | shareConfigUpdates.codeBlockToolbarPosition = codeBlockToolbarPosition; |
| 151 | migratedUI = withoutToolbarPosition; |
| 152 | effected = true; |
| 153 | } |
| 154 | |
| 155 | const { fontSize, ...withoutFontSize } = migratedUI; |
| 156 | if (fontSize !== undefined) { |
| 157 | shareConfigUpdates.fontSize = fontSize; |
| 158 | migratedUI = withoutFontSize; |
| 159 | effected = true; |
| 160 | } |
| 161 | |
| 162 | const { codeWrap, ...withoutCodeWrap } = migratedUI; |
| 163 | if (codeWrap !== undefined) { |
| 164 | shareConfigUpdates.codeWrap = codeWrap; |
| 165 | migratedUI = withoutCodeWrap; |
| 166 | effected = true; |
| 167 | } |
| 168 | |
| 169 | const { displayRawMarkdown, ...withoutMD } = migratedUI; |
| 170 | if (displayRawMarkdown !== undefined) { |
| 171 | shareConfigUpdates.displayRawMarkdown = displayRawMarkdown; |
| 172 | migratedUI = withoutMD; |
| 173 | effected = true; |
| 174 | } |
| 175 | |
| 176 | const { autoAcceptEditToolDiffs, ...withoutAutoApply } = migratedUI; |
| 177 | if (autoAcceptEditToolDiffs !== undefined) { |
| 178 | shareConfigUpdates.autoAcceptEditToolDiffs = autoAcceptEditToolDiffs; |
| 179 | migratedUI = withoutAutoApply; |
| 180 | effected = true; |
| 181 | } |
| 182 | |
| 183 | const { showChatScrollbar, ...withoutShowChatScrollbar } = migratedUI; |
| 184 | if (showChatScrollbar !== undefined) { |
| 185 | shareConfigUpdates.showChatScrollbar = showChatScrollbar; |
| 186 | migratedUI = withoutShowChatScrollbar; |
| 187 | effected = true; |
| 188 | } |
| 189 | |
| 190 | // Ancient param to overwrite disableSessionTitles |
| 191 | if ("getChatTitles" in migratedUI) { |
| 192 | const { getChatTitles, ...withoutChatTitles } = migratedUI; |
| 193 | if (getChatTitles === false) { |
| 194 | shareConfigUpdates.disableSessionTitles = true; |
| 195 | migratedUI = withoutChatTitles; |
| 196 | effected = true; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (Object.keys(migratedUI).length > 0) { |
| 201 | config = { |
| 202 | ...withoutUI, |
| 203 | ui: migratedUI, |
| 204 | }; |
| 205 | } else { |
| 206 | config = withoutUI; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (effected) { |
| 211 | new GlobalContext().updateSharedConfig(shareConfigUpdates); |
| 212 | } |
| 213 | } catch (e) { |
| 214 | console.error(`Migration: Failed to parse config.json: ${e}`); |
| 215 | } |
| 216 | } |