| 1 | import * as fs from "fs"; |
| 2 | import * as os from "os"; |
| 3 | import * as path from "path"; |
| 4 | import * as YAML from "yaml"; |
| 5 | |
| 6 | import { ConfigYaml, DevEventName } from "@continuedev/config-yaml"; |
| 7 | import * as JSONC from "comment-json"; |
| 8 | import dotenv from "dotenv"; |
| 9 | |
| 10 | import { IdeType, SerializedContinueConfig } from "../"; |
| 11 | import { defaultConfig, defaultConfigJetBrains } from "../config/default"; |
| 12 | import Types from "../config/types"; |
| 13 | |
| 14 | dotenv.config(); |
| 15 | |
| 16 | const CONTINUE_GLOBAL_DIR = (() => { |
| 17 | const configPath = process.env.CONTINUE_GLOBAL_DIR; |
| 18 | if (configPath) { |
| 19 | // Convert relative path to absolute paths based on current working directory |
| 20 | return path.isAbsolute(configPath) |
| 21 | ? configPath |
| 22 | : path.resolve(process.cwd(), configPath); |
| 23 | } |
| 24 | return path.join(os.homedir(), ".continue"); |
| 25 | })(); |
| 26 | |
| 27 | // export const DEFAULT_CONFIG_TS_CONTENTS = `import { Config } from "./types"\n\nexport function modifyConfig(config: Config): Config { |
| 28 | // return config; |
| 29 | // }`; |
| 30 | |
| 31 | export const DEFAULT_CONFIG_TS_CONTENTS = `export function modifyConfig(config: Config): Config { |
| 32 | return config; |
| 33 | }`; |
| 34 | |
| 35 | export function getChromiumPath(): string { |
| 36 | return path.join(getContinueUtilsPath(), ".chromium-browser-snapshots"); |
| 37 | } |
| 38 | |
| 39 | export function getContinueUtilsPath(): string { |
| 40 | const utilsPath = path.join(getContinueGlobalPath(), ".utils"); |
| 41 | if (!fs.existsSync(utilsPath)) { |
| 42 | fs.mkdirSync(utilsPath); |
| 43 | } |
| 44 | return utilsPath; |
| 45 | } |
| 46 | |
| 47 | export function getGlobalContinueIgnorePath(): string { |
| 48 | const continueIgnorePath = path.join( |
| 49 | getContinueGlobalPath(), |
| 50 | ".continueignore", |
| 51 | ); |
| 52 | if (!fs.existsSync(continueIgnorePath)) { |
| 53 | fs.writeFileSync(continueIgnorePath, ""); |
| 54 | } |
| 55 | return continueIgnorePath; |
| 56 | } |
| 57 | |
| 58 | export function getContinueGlobalPath(): string { |
| 59 | // This is ~/.continue on mac/linux |
| 60 | const continuePath = CONTINUE_GLOBAL_DIR; |
| 61 | if (!fs.existsSync(continuePath)) { |
| 62 | fs.mkdirSync(continuePath); |
| 63 | } |
| 64 | return continuePath; |
| 65 | } |
| 66 | |
| 67 | export function getSessionsFolderPath(): string { |
| 68 | const sessionsPath = path.join(getContinueGlobalPath(), "sessions"); |
| 69 | if (!fs.existsSync(sessionsPath)) { |
| 70 | fs.mkdirSync(sessionsPath); |
| 71 | } |
| 72 | return sessionsPath; |
| 73 | } |
| 74 | |
| 75 | export function getIndexFolderPath(): string { |
| 76 | const indexPath = path.join(getContinueGlobalPath(), "index"); |
| 77 | if (!fs.existsSync(indexPath)) { |
| 78 | fs.mkdirSync(indexPath); |
| 79 | } |
| 80 | return indexPath; |
| 81 | } |
| 82 | |
| 83 | export function getGlobalContextFilePath(): string { |
| 84 | return path.join(getIndexFolderPath(), "globalContext.json"); |
| 85 | } |
| 86 | |
| 87 | export function getSharedConfigFilePath(): string { |
| 88 | return path.join(getContinueGlobalPath(), "sharedConfig.json"); |
| 89 | } |
| 90 | |
| 91 | export function getSessionFilePath(sessionId: string): string { |
| 92 | return path.join(getSessionsFolderPath(), `${sessionId}.json`); |
| 93 | } |
| 94 | |
| 95 | export function getSessionsListPath(): string { |
| 96 | const filepath = path.join(getSessionsFolderPath(), "sessions.json"); |
| 97 | if (!fs.existsSync(filepath)) { |
| 98 | fs.writeFileSync(filepath, JSON.stringify([])); |
| 99 | } |
| 100 | return filepath; |
| 101 | } |
| 102 | |
| 103 | export function getConfigJsonPath(): string { |
| 104 | const p = path.join(getContinueGlobalPath(), "config.json"); |
| 105 | return p; |
| 106 | } |
| 107 | |
| 108 | export function getConfigYamlPath(ideType?: IdeType): string { |
| 109 | const p = path.join(getContinueGlobalPath(), "config.yaml"); |
| 110 | if (!fs.existsSync(p) && !fs.existsSync(getConfigJsonPath())) { |
| 111 | if (ideType === "jetbrains") { |
| 112 | fs.writeFileSync(p, YAML.stringify(defaultConfigJetBrains)); |
| 113 | } else { |
| 114 | fs.writeFileSync(p, YAML.stringify(defaultConfig)); |
| 115 | } |
| 116 | } |
| 117 | return p; |
| 118 | } |
| 119 | |
| 120 | export function getPrimaryConfigFilePath(): string { |
| 121 | const configYamlPath = getConfigYamlPath(); |
| 122 | if (fs.existsSync(configYamlPath)) { |
| 123 | return configYamlPath; |
| 124 | } |
| 125 | return getConfigJsonPath(); |
| 126 | } |
| 127 | |
| 128 | export function getConfigTsPath(): string { |
| 129 | const p = path.join(getContinueGlobalPath(), "config.ts"); |
| 130 | if (!fs.existsSync(p)) { |
| 131 | fs.writeFileSync(p, DEFAULT_CONFIG_TS_CONTENTS); |
| 132 | } |
| 133 | |
| 134 | const typesPath = path.join(getContinueGlobalPath(), "types"); |
| 135 | if (!fs.existsSync(typesPath)) { |
| 136 | fs.mkdirSync(typesPath); |
| 137 | } |
| 138 | const corePath = path.join(typesPath, "core"); |
| 139 | if (!fs.existsSync(corePath)) { |
| 140 | fs.mkdirSync(corePath); |
| 141 | } |
| 142 | const packageJsonPath = path.join(getContinueGlobalPath(), "package.json"); |
| 143 | if (!fs.existsSync(packageJsonPath)) { |
| 144 | fs.writeFileSync( |
| 145 | packageJsonPath, |
| 146 | JSON.stringify({ |
| 147 | name: "continue-config", |
| 148 | version: "1.0.0", |
| 149 | description: "My Continue Configuration", |
| 150 | main: "config.js", |
| 151 | }), |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | fs.writeFileSync(path.join(corePath, "index.d.ts"), Types); |
| 156 | return p; |
| 157 | } |
| 158 | |
| 159 | export function getConfigJsPath(): string { |
| 160 | // Do not create automatically |
| 161 | return path.join(getContinueGlobalPath(), "out", "config.js"); |
| 162 | } |
| 163 | |
| 164 | export function getTsConfigPath(): string { |
| 165 | const tsConfigPath = path.join(getContinueGlobalPath(), "tsconfig.json"); |
| 166 | if (!fs.existsSync(tsConfigPath)) { |
| 167 | fs.writeFileSync( |
| 168 | tsConfigPath, |
| 169 | JSON.stringify( |
| 170 | { |
| 171 | compilerOptions: { |
| 172 | target: "ESNext", |
| 173 | useDefineForClassFields: true, |
| 174 | lib: ["DOM", "DOM.Iterable", "ESNext"], |
| 175 | allowJs: true, |
| 176 | skipLibCheck: true, |
| 177 | esModuleInterop: false, |
| 178 | allowSyntheticDefaultImports: true, |
| 179 | strict: true, |
| 180 | forceConsistentCasingInFileNames: true, |
| 181 | module: "System", |
| 182 | moduleResolution: "Node", |
| 183 | noEmit: false, |
| 184 | noEmitOnError: false, |
| 185 | outFile: "./out/config.js", |
| 186 | typeRoots: ["./node_modules/@types", "./types"], |
| 187 | }, |
| 188 | include: ["./config.ts"], |
| 189 | }, |
| 190 | null, |
| 191 | 2, |
| 192 | ), |
| 193 | ); |
| 194 | } |
| 195 | return tsConfigPath; |
| 196 | } |
| 197 | |
| 198 | export function getContinueRcPath(): string { |
| 199 | // Disable indexing of the config folder to prevent infinite loops |
| 200 | const continuercPath = path.join(getContinueGlobalPath(), ".continuerc.json"); |
| 201 | if (!fs.existsSync(continuercPath)) { |
| 202 | fs.writeFileSync( |
| 203 | continuercPath, |
| 204 | JSON.stringify( |
| 205 | { |
| 206 | disableIndexing: true, |
| 207 | }, |
| 208 | null, |
| 209 | 2, |
| 210 | ), |
| 211 | ); |
| 212 | } |
| 213 | return continuercPath; |
| 214 | } |
| 215 | |
| 216 | function getDevDataPath(): string { |
| 217 | const sPath = path.join(getContinueGlobalPath(), "dev_data"); |
| 218 | if (!fs.existsSync(sPath)) { |
| 219 | fs.mkdirSync(sPath); |
| 220 | } |
| 221 | return sPath; |
| 222 | } |
| 223 | |
| 224 | export function getDevDataSqlitePath(): string { |
| 225 | return path.join(getDevDataPath(), "devdata.sqlite"); |
| 226 | } |
| 227 | |
| 228 | export function getDevDataFilePath( |
| 229 | eventName: DevEventName, |
| 230 | schema: string, |
| 231 | ): string { |
| 232 | const versionPath = path.join(getDevDataPath(), schema); |
| 233 | if (!fs.existsSync(versionPath)) { |
| 234 | fs.mkdirSync(versionPath); |
| 235 | } |
| 236 | return path.join(versionPath, `${String(eventName)}.jsonl`); |
| 237 | } |
| 238 | |
| 239 | function editConfigJson( |
| 240 | callback: (config: SerializedContinueConfig) => SerializedContinueConfig, |
| 241 | ): void { |
| 242 | const config = fs.readFileSync(getConfigJsonPath(), "utf8"); |
| 243 | let configJson = JSONC.parse(config); |
| 244 | // Check if it's an object |
| 245 | if (typeof configJson === "object" && configJson !== null) { |
| 246 | configJson = callback(configJson as any) as any; |
| 247 | fs.writeFileSync(getConfigJsonPath(), JSONC.stringify(configJson, null, 2)); |
| 248 | } else { |
| 249 | console.warn("config.json is not a valid object"); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | function editConfigYaml(callback: (config: ConfigYaml) => ConfigYaml): void { |
| 254 | const config = fs.readFileSync(getConfigYamlPath(), "utf8"); |
| 255 | let configYaml = YAML.parse(config); |
| 256 | // Check if it's an object |
| 257 | if (typeof configYaml === "object" && configYaml !== null) { |
| 258 | configYaml = callback(configYaml as any) as any; |
| 259 | fs.writeFileSync(getConfigYamlPath(), YAML.stringify(configYaml)); |
| 260 | } else { |
| 261 | console.warn("config.yaml is not a valid object"); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | export function editConfigFile( |
| 266 | configJsonCallback: ( |
| 267 | config: SerializedContinueConfig, |
| 268 | ) => SerializedContinueConfig, |
| 269 | configYamlCallback: (config: ConfigYaml) => ConfigYaml, |
| 270 | ): void { |
| 271 | if (fs.existsSync(getConfigYamlPath())) { |
| 272 | editConfigYaml(configYamlCallback); |
| 273 | } else if (fs.existsSync(getConfigJsonPath())) { |
| 274 | editConfigJson(configJsonCallback); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | function getMigrationsFolderPath(): string { |
| 279 | const migrationsPath = path.join(getContinueGlobalPath(), ".migrations"); |
| 280 | if (!fs.existsSync(migrationsPath)) { |
| 281 | fs.mkdirSync(migrationsPath); |
| 282 | } |
| 283 | return migrationsPath; |
| 284 | } |
| 285 | |
| 286 | export async function migrate( |
| 287 | id: string, |
| 288 | callback: () => void | Promise<void>, |
| 289 | onAlreadyComplete?: () => void, |
| 290 | ) { |
| 291 | if (process.env.NODE_ENV === "test") { |
| 292 | return await Promise.resolve(callback()); |
| 293 | } |
| 294 | |
| 295 | const migrationsPath = getMigrationsFolderPath(); |
| 296 | const migrationPath = path.join(migrationsPath, id); |
| 297 | |
| 298 | if (!fs.existsSync(migrationPath)) { |
| 299 | try { |
| 300 | console.log(`Running migration: ${id}`); |
| 301 | |
| 302 | fs.writeFileSync(migrationPath, ""); |
| 303 | await Promise.resolve(callback()); |
| 304 | } catch (e) { |
| 305 | console.warn(`Migration ${id} failed`, e); |
| 306 | } |
| 307 | } else if (onAlreadyComplete) { |
| 308 | onAlreadyComplete(); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | export function getIndexSqlitePath(): string { |
| 313 | return path.join(getIndexFolderPath(), "index.sqlite"); |
| 314 | } |
| 315 | |
| 316 | export function getLanceDbPath(): string { |
| 317 | return path.join(getIndexFolderPath(), "lancedb"); |
| 318 | } |
| 319 | |
| 320 | export function getTabAutocompleteCacheSqlitePath(): string { |
| 321 | return path.join(getIndexFolderPath(), "autocompleteCache.sqlite"); |
| 322 | } |
| 323 | |
| 324 | export function getDocsSqlitePath(): string { |
| 325 | return path.join(getIndexFolderPath(), "docs.sqlite"); |
| 326 | } |
| 327 | |
| 328 | export function getRemoteConfigsFolderPath(): string { |
| 329 | const dir = path.join(getContinueGlobalPath(), ".configs"); |
| 330 | if (!fs.existsSync(dir)) { |
| 331 | fs.mkdirSync(dir); |
| 332 | } |
| 333 | return dir; |
| 334 | } |
| 335 | |
| 336 | export function getPathToRemoteConfig(remoteConfigServerUrl: string): string { |
| 337 | let url: URL | undefined = undefined; |
| 338 | try { |
| 339 | url = |
| 340 | typeof remoteConfigServerUrl !== "string" || remoteConfigServerUrl === "" |
| 341 | ? undefined |
| 342 | : new URL(remoteConfigServerUrl); |
| 343 | } catch (e) {} |
| 344 | const dir = path.join(getRemoteConfigsFolderPath(), url?.hostname ?? "None"); |
| 345 | if (!fs.existsSync(dir)) { |
| 346 | fs.mkdirSync(dir); |
| 347 | } |
| 348 | return dir; |
| 349 | } |
| 350 | |
| 351 | export function getConfigJsonPathForRemote( |
| 352 | remoteConfigServerUrl: string, |
| 353 | ): string { |
| 354 | return path.join(getPathToRemoteConfig(remoteConfigServerUrl), "config.json"); |
| 355 | } |
| 356 | |
| 357 | export function getConfigJsPathForRemote( |
| 358 | remoteConfigServerUrl: string, |
| 359 | ): string { |
| 360 | return path.join(getPathToRemoteConfig(remoteConfigServerUrl), "config.js"); |
| 361 | } |
| 362 | |
| 363 | export function getContinueDotEnv(): { [key: string]: string } { |
| 364 | const filepath = path.join(getContinueGlobalPath(), ".env"); |
| 365 | if (fs.existsSync(filepath)) { |
| 366 | return dotenv.parse(fs.readFileSync(filepath)); |
| 367 | } |
| 368 | return {}; |
| 369 | } |
| 370 | |
| 371 | export function getLogsDirPath(): string { |
| 372 | const logsPath = path.join(getContinueGlobalPath(), "logs"); |
| 373 | if (!fs.existsSync(logsPath)) { |
| 374 | fs.mkdirSync(logsPath); |
| 375 | } |
| 376 | return logsPath; |
| 377 | } |
| 378 | |
| 379 | export function getCoreLogsPath(): string { |
| 380 | return path.join(getLogsDirPath(), "core.log"); |
| 381 | } |
| 382 | |
| 383 | export function getPromptLogsPath(): string { |
| 384 | return path.join(getLogsDirPath(), "prompt.log"); |
| 385 | } |
| 386 | |
| 387 | export function getGlobalFolderWithName(name: string): string { |
| 388 | return path.join(getContinueGlobalPath(), name); |
| 389 | } |
| 390 | |
| 391 | export function getGlobalPromptsPath(): string { |
| 392 | return getGlobalFolderWithName("prompts"); |
| 393 | } |
| 394 | |
| 395 | export function getGlobalAssistantsPath(): string { |
| 396 | return getGlobalFolderWithName("assistants"); |
| 397 | } |
| 398 | |
| 399 | export function readAllGlobalPromptFiles( |
| 400 | folderPath: string = getGlobalPromptsPath(), |
| 401 | ): { path: string; content: string }[] { |
| 402 | if (!fs.existsSync(folderPath)) { |
| 403 | return []; |
| 404 | } |
| 405 | const files = fs.readdirSync(folderPath); |
| 406 | const promptFiles: { path: string; content: string }[] = []; |
| 407 | files.forEach((file) => { |
| 408 | const filepath = path.join(folderPath, file); |
| 409 | const stats = fs.statSync(filepath); |
| 410 | |
| 411 | if (stats.isDirectory()) { |
| 412 | const nestedPromptFiles = readAllGlobalPromptFiles(filepath); |
| 413 | promptFiles.push(...nestedPromptFiles); |
| 414 | } else if (file.endsWith(".prompt")) { |
| 415 | const content = fs.readFileSync(filepath, "utf8"); |
| 416 | promptFiles.push({ path: filepath, content }); |
| 417 | } |
| 418 | }); |
| 419 | |
| 420 | return promptFiles; |
| 421 | } |
| 422 | |
| 423 | export function getRepoMapFilePath(): string { |
| 424 | return path.join(getContinueUtilsPath(), "repo_map.txt"); |
| 425 | } |
| 426 | |
| 427 | export function getEsbuildBinaryPath(): string { |
| 428 | return path.join(getContinueUtilsPath(), "esbuild"); |
| 429 | } |
| 430 | |
| 431 | export function migrateV1DevDataFiles() { |
| 432 | const devDataPath = getDevDataPath(); |
| 433 | function moveToV1FolderIfExists( |
| 434 | oldFileName: string, |
| 435 | newFileName: DevEventName, |
| 436 | ) { |
| 437 | const oldFilePath = path.join(devDataPath, `${oldFileName}.jsonl`); |
| 438 | if (fs.existsSync(oldFilePath)) { |
| 439 | const newFilePath = getDevDataFilePath(newFileName, "0.1.0"); |
| 440 | if (!fs.existsSync(newFilePath)) { |
| 441 | fs.copyFileSync(oldFilePath, newFilePath); |
| 442 | fs.unlinkSync(oldFilePath); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | moveToV1FolderIfExists("tokens_generated", "tokensGenerated"); |
| 447 | moveToV1FolderIfExists("chat", "chatFeedback"); |
| 448 | moveToV1FolderIfExists("quickEdit", "quickEdit"); |
| 449 | moveToV1FolderIfExists("autocomplete", "autocomplete"); |
| 450 | } |
| 451 | |
| 452 | export function getLocalEnvironmentDotFilePath(): string { |
| 453 | return path.join(getContinueGlobalPath(), ".local"); |
| 454 | } |
| 455 | |
| 456 | export function getStagingEnvironmentDotFilePath(): string { |
| 457 | return path.join(getContinueGlobalPath(), ".staging"); |
| 458 | } |
| 459 | |
| 460 | export function getDiffsDirectoryPath(): string { |
| 461 | const diffsPath = path.join(getContinueGlobalPath(), ".diffs"); // .replace(/^C:/, "c:"); ?? |
| 462 | if (!fs.existsSync(diffsPath)) { |
| 463 | fs.mkdirSync(diffsPath, { |
| 464 | recursive: true, |
| 465 | }); |
| 466 | } |
| 467 | return diffsPath; |
| 468 | } |