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