| 1 | import z from "zod"; |
| 2 | |
| 3 | import { |
| 4 | BrowserSerializedContinueConfig, |
| 5 | Config, |
| 6 | ContinueConfig, |
| 7 | SerializedContinueConfig, |
| 8 | } from ".."; |
| 9 | |
| 10 | export const sharedConfigSchema = z |
| 11 | .object({ |
| 12 | // boolean fields in config.json |
| 13 | allowAnonymousTelemetry: z.boolean(), |
| 14 | disableIndexing: z.boolean(), |
| 15 | disableSessionTitles: z.boolean(), |
| 16 | |
| 17 | // `experimental` in `ContinueConfig` |
| 18 | useChromiumForDocsCrawling: z.boolean(), |
| 19 | readResponseTTS: z.boolean(), |
| 20 | promptPath: z.string(), |
| 21 | useCurrentFileAsContext: z.boolean(), |
| 22 | enableExperimentalTools: z.boolean(), |
| 23 | onlyUseSystemMessageTools: z.boolean(), |
| 24 | codebaseToolCallingOnly: z.boolean(), |
| 25 | enableStaticContextualization: z.boolean(), |
| 26 | |
| 27 | // `ui` in `ContinueConfig` |
| 28 | showSessionTabs: z.boolean(), |
| 29 | codeBlockToolbarPosition: z.enum(["top", "bottom"]), |
| 30 | fontSize: z.number(), |
| 31 | codeWrap: z.boolean(), |
| 32 | displayRawMarkdown: z.boolean(), |
| 33 | showChatScrollbar: z.boolean(), |
| 34 | autoAcceptEditToolDiffs: z.boolean(), |
| 35 | continueAfterToolRejection: z.boolean(), |
| 36 | |
| 37 | // `tabAutocompleteOptions` in `ContinueConfig` |
| 38 | useAutocompleteCache: z.boolean(), |
| 39 | useAutocompleteMultilineCompletions: z.enum(["always", "never", "auto"]), |
| 40 | disableAutocompleteInFiles: z.array(z.string()), |
| 41 | modelTimeout: z.number(), |
| 42 | debounceDelay: z.number(), |
| 43 | }) |
| 44 | .partial(); |
| 45 | |
| 46 | export type SharedConfigSchema = z.infer<typeof sharedConfigSchema>; |
| 47 | |
| 48 | // For security in case of damaged config file, try to salvage any security-related values |
| 49 | export function salvageSharedConfig(sharedConfig: object): SharedConfigSchema { |
| 50 | const salvagedConfig: SharedConfigSchema = {}; |
| 51 | if ("allowAnonymousTelemetry" in sharedConfig) { |
| 52 | const val = z.boolean().safeParse(sharedConfig.allowAnonymousTelemetry); |
| 53 | if (val.success) { |
| 54 | salvagedConfig.allowAnonymousTelemetry = val.data; |
| 55 | } |
| 56 | } |
| 57 | if ("disableIndexing" in sharedConfig) { |
| 58 | const val = z.boolean().safeParse(sharedConfig.disableIndexing); |
| 59 | if (val.success) { |
| 60 | salvagedConfig.disableIndexing = val.data; |
| 61 | } |
| 62 | } |
| 63 | if ("disableSessionTitles" in sharedConfig) { |
| 64 | const val = z.boolean().safeParse(sharedConfig.disableSessionTitles); |
| 65 | if (val.success) { |
| 66 | salvagedConfig.disableSessionTitles = val.data; |
| 67 | } |
| 68 | } |
| 69 | if ("disableAutocompleteInFiles" in sharedConfig) { |
| 70 | const val = sharedConfigSchema.shape.disableAutocompleteInFiles.safeParse( |
| 71 | sharedConfig.disableAutocompleteInFiles, |
| 72 | ); |
| 73 | if (val.success) { |
| 74 | salvagedConfig.disableAutocompleteInFiles = val.data; |
| 75 | } |
| 76 | } |
| 77 | return salvagedConfig; |
| 78 | } |
| 79 | |
| 80 | // Apply shared config to all forms of config |
| 81 | // - SerializedContinueConfig (config.json) |
| 82 | // - Config ("intermediate") - passed to config.ts |
| 83 | // - ContinueConfig |
| 84 | // - BrowserSerializedContinueConfig (final converted to be passed to GUI) |
| 85 | |
| 86 | // This modify function is split into two steps |
| 87 | // - rectifySharedModelsFromSharedConfig - includes boolean flags like allowAnonymousTelemetry which |
| 88 | // must be added BEFORE config.ts and remote server config apply for JSON |
| 89 | // for security reasons |
| 90 | // - setSharedModelsFromSharedConfig - exists because of selectedModelsByRole |
| 91 | // Which don't exist on SerializedContinueConfig/Config types, so must be added after the fact |
| 92 | export function modifyAnyConfigWithSharedConfig< |
| 93 | T extends |
| 94 | | ContinueConfig |
| 95 | | BrowserSerializedContinueConfig |
| 96 | | Config |
| 97 | | SerializedContinueConfig, |
| 98 | >(continueConfig: T, sharedConfig: SharedConfigSchema): T { |
| 99 | const configCopy = { ...continueConfig }; |
| 100 | configCopy.tabAutocompleteOptions = { |
| 101 | ...configCopy.tabAutocompleteOptions, |
| 102 | }; |
| 103 | if (sharedConfig.useAutocompleteCache !== undefined) { |
| 104 | configCopy.tabAutocompleteOptions.useCache = |
| 105 | sharedConfig.useAutocompleteCache; |
| 106 | } |
| 107 | if (sharedConfig.useAutocompleteMultilineCompletions !== undefined) { |
| 108 | configCopy.tabAutocompleteOptions.multilineCompletions = |
| 109 | sharedConfig.useAutocompleteMultilineCompletions; |
| 110 | } |
| 111 | if (sharedConfig.disableAutocompleteInFiles !== undefined) { |
| 112 | configCopy.tabAutocompleteOptions.disableInFiles = |
| 113 | sharedConfig.disableAutocompleteInFiles; |
| 114 | } |
| 115 | if (sharedConfig.modelTimeout !== undefined) { |
| 116 | configCopy.tabAutocompleteOptions.modelTimeout = sharedConfig.modelTimeout; |
| 117 | } |
| 118 | if (sharedConfig.debounceDelay !== undefined) { |
| 119 | configCopy.tabAutocompleteOptions.debounceDelay = |
| 120 | sharedConfig.debounceDelay; |
| 121 | } |
| 122 | |
| 123 | configCopy.ui = { |
| 124 | ...configCopy.ui, |
| 125 | }; |
| 126 | |
| 127 | if (sharedConfig.codeBlockToolbarPosition !== undefined) { |
| 128 | configCopy.ui.codeBlockToolbarPosition = |
| 129 | sharedConfig.codeBlockToolbarPosition; |
| 130 | } |
| 131 | if (sharedConfig.fontSize !== undefined) { |
| 132 | configCopy.ui.fontSize = sharedConfig.fontSize; |
| 133 | } |
| 134 | if (sharedConfig.codeWrap !== undefined) { |
| 135 | configCopy.ui.codeWrap = sharedConfig.codeWrap; |
| 136 | } |
| 137 | if (sharedConfig.displayRawMarkdown !== undefined) { |
| 138 | configCopy.ui.displayRawMarkdown = sharedConfig.displayRawMarkdown; |
| 139 | } |
| 140 | if (sharedConfig.showChatScrollbar !== undefined) { |
| 141 | configCopy.ui.showChatScrollbar = sharedConfig.showChatScrollbar; |
| 142 | } |
| 143 | if (sharedConfig.autoAcceptEditToolDiffs !== undefined) { |
| 144 | configCopy.ui.autoAcceptEditToolDiffs = |
| 145 | sharedConfig.autoAcceptEditToolDiffs; |
| 146 | } |
| 147 | |
| 148 | if (sharedConfig.allowAnonymousTelemetry !== undefined) { |
| 149 | configCopy.allowAnonymousTelemetry = sharedConfig.allowAnonymousTelemetry; |
| 150 | } |
| 151 | if (sharedConfig.disableIndexing !== undefined) { |
| 152 | configCopy.disableIndexing = sharedConfig.disableIndexing; |
| 153 | } |
| 154 | if (sharedConfig.disableSessionTitles !== undefined) { |
| 155 | configCopy.disableSessionTitles = sharedConfig.disableSessionTitles; |
| 156 | } |
| 157 | |
| 158 | if (sharedConfig.showSessionTabs !== undefined) { |
| 159 | configCopy.ui.showSessionTabs = sharedConfig.showSessionTabs; |
| 160 | } |
| 161 | |
| 162 | if (sharedConfig.continueAfterToolRejection !== undefined) { |
| 163 | configCopy.ui.continueAfterToolRejection = |
| 164 | sharedConfig.continueAfterToolRejection; |
| 165 | } |
| 166 | |
| 167 | configCopy.experimental = { |
| 168 | ...configCopy.experimental, |
| 169 | }; |
| 170 | |
| 171 | if (sharedConfig.enableExperimentalTools !== undefined) { |
| 172 | configCopy.experimental.enableExperimentalTools = |
| 173 | sharedConfig.enableExperimentalTools; |
| 174 | } |
| 175 | |
| 176 | if (sharedConfig.promptPath !== undefined) { |
| 177 | configCopy.experimental.promptPath = sharedConfig.promptPath; |
| 178 | } |
| 179 | if (sharedConfig.useChromiumForDocsCrawling !== undefined) { |
| 180 | configCopy.experimental.useChromiumForDocsCrawling = |
| 181 | sharedConfig.useChromiumForDocsCrawling; |
| 182 | } |
| 183 | if (sharedConfig.readResponseTTS !== undefined) { |
| 184 | configCopy.experimental.readResponseTTS = sharedConfig.readResponseTTS; |
| 185 | } |
| 186 | if (sharedConfig.useCurrentFileAsContext !== undefined) { |
| 187 | configCopy.experimental.useCurrentFileAsContext = |
| 188 | sharedConfig.useCurrentFileAsContext; |
| 189 | } |
| 190 | |
| 191 | if (sharedConfig.onlyUseSystemMessageTools !== undefined) { |
| 192 | configCopy.experimental.onlyUseSystemMessageTools = |
| 193 | sharedConfig.onlyUseSystemMessageTools; |
| 194 | } |
| 195 | |
| 196 | if (sharedConfig.codebaseToolCallingOnly !== undefined) { |
| 197 | configCopy.experimental.codebaseToolCallingOnly = |
| 198 | sharedConfig.codebaseToolCallingOnly; |
| 199 | } |
| 200 | if (sharedConfig.enableStaticContextualization !== undefined) { |
| 201 | configCopy.experimental.enableStaticContextualization = |
| 202 | sharedConfig.enableStaticContextualization; |
| 203 | } |
| 204 | |
| 205 | return configCopy; |
| 206 | } |