| 1 | import * as fs from "fs"; |
| 2 | import * as os from "os"; |
| 3 | import * as path from "path"; |
| 4 | import * as URI from "uri-js"; |
| 5 | import * as YAML from "yaml"; |
| 6 | |
| 7 | import { ConfigYaml, DevEventName } from "@continuedev/config-yaml"; |
| 8 | import * as JSONC from "comment-json"; |
| 9 | import dotenv from "dotenv"; |
| 10 | |
| 11 | import { IdeType, SerializedContinueConfig } from "../"; |
| 12 | import { defaultConfig } from "../config/default"; |
| 13 | import Types from "../config/types"; |
| 14 | |
| 15 | dotenv.config(); |
| 16 | |
| 17 | const CONTINUE_GLOBAL_DIR = (() => { |
| 18 | const configPath = process.env.CONTINUE_GLOBAL_DIR; |
| 19 | if (configPath) { |
| 20 | // Convert relative path to absolute paths based on current working directory |
| 21 | return path.isAbsolute(configPath) |
| 22 | ? configPath |
| 23 | : path.resolve(process.cwd(), configPath); |
| 24 | } |
| 25 | return path.join(os.homedir(), ".continue"); |
| 26 | })(); |
| 27 | |
| 28 | // export const DEFAULT_CONFIG_TS_CONTENTS = `import { Config } from "./types"\n\nexport function modifyConfig(config: Config): Config { |
| 29 | // return config; |
| 30 | // }`; |
| 31 | |
| 32 | export const DEFAULT_CONFIG_TS_CONTENTS = `export function modifyConfig(config: Config): Config { |
| 33 | return config; |
| 34 | }`; |
| 35 | |
| 36 | export function getChromiumPath(): string { |
| 37 | return path.join(getContinueUtilsPath(), ".chromium-browser-snapshots"); |
| 38 | } |
| 39 | |
| 40 | export function getContinueUtilsPath(): string { |
| 41 | const utilsPath = path.join(getContinueGlobalPath(), ".utils"); |
| 42 | if (!fs.existsSync(utilsPath)) { |
| 43 | fs.mkdirSync(utilsPath); |
| 44 | } |
| 45 | return utilsPath; |
| 46 | } |
| 47 | |
| 48 | export function getGlobalContinueIgnorePath(): string { |
| 49 | const continueIgnorePath = path.join( |
| 50 | getContinueGlobalPath(), |
| 51 | ".continueignore", |
| 52 | ); |
| 53 | if (!fs.existsSync(continueIgnorePath)) { |
| 54 | fs.writeFileSync(continueIgnorePath, ""); |
| 55 | } |
| 56 | return continueIgnorePath; |
| 57 | } |
| 58 | |
| 59 | export function getContinueGlobalPath(): string { |
| 60 | // This is ~/.continue on mac/linux |
| 61 | const continuePath = CONTINUE_GLOBAL_DIR; |
| 62 | if (!fs.existsSync(continuePath)) { |
| 63 | fs.mkdirSync(continuePath); |
| 64 | } |
| 65 | return continuePath; |
| 66 | } |
| 67 | |
| 68 | export function getSessionsFolderPath(): string { |
| 69 | const sessionsPath = path.join(getContinueGlobalPath(), "sessions"); |
| 70 | if (!fs.existsSync(sessionsPath)) { |
| 71 | fs.mkdirSync(sessionsPath); |
| 72 | } |
| 73 | return sessionsPath; |
| 74 | } |
| 75 | |
| 76 | export function getIndexFolderPath(): string { |
| 77 | const indexPath = path.join(getContinueGlobalPath(), "index"); |
| 78 | if (!fs.existsSync(indexPath)) { |
| 79 | fs.mkdirSync(indexPath); |
| 80 | } |
| 81 | return indexPath; |
| 82 | } |
| 83 | |
| 84 | export function getGlobalContextFilePath(): string { |
| 85 | return path.join(getIndexFolderPath(), "globalContext.json"); |
| 86 | } |
| 87 | |
| 88 | export function getSharedConfigFilePath(): string { |
| 89 | return path.join(getContinueGlobalPath(), "sharedConfig.json"); |
| 90 | } |
| 91 | |
| 92 | export function getSessionFilePath(sessionId: string): string { |
| 93 | return path.join(getSessionsFolderPath(), `${sessionId}.json`); |
| 94 | } |
| 95 | |
| 96 | export function getSessionsListPath(): string { |
| 97 | const filepath = path.join(getSessionsFolderPath(), "sessions.json"); |
| 98 | if (!fs.existsSync(filepath)) { |
| 99 | fs.writeFileSync(filepath, JSON.stringify([])); |
| 100 | } |
| 101 | return filepath; |
| 102 | } |
| 103 | |
| 104 | export function getConfigJsonPath(): string { |
| 105 | const p = path.join(getContinueGlobalPath(), "config.json"); |
| 106 | return p; |
| 107 | } |
| 108 | |
| 109 | export function getConfigYamlPath(ideType?: IdeType): string { |
| 110 | const p = path.join(getContinueGlobalPath(), "config.yaml"); |
| 111 | if (!fs.existsSync(p) && !fs.existsSync(getConfigJsonPath())) { |
| 112 | if (ideType === "jetbrains") { |
| 113 | // https://github.com/continuedev/continue/pull/7224 |
| 114 | // This was here because we had different context provider support between jetbrains and vs code |
| 115 | // Leaving so we could differentiate later but for now configs are the same between IDEs |
| 116 | fs.writeFileSync(p, YAML.stringify(defaultConfig)); |
| 117 | } else { |
| 118 | fs.writeFileSync(p, YAML.stringify(defaultConfig)); |
| 119 | } |
| 120 | } |
| 121 | return p; |
| 122 | } |
| 123 | |
| 124 | export function getPrimaryConfigFilePath(): string { |
| 125 | const configYamlPath = getConfigYamlPath(); |
| 126 | if (fs.existsSync(configYamlPath)) { |
| 127 | return configYamlPath; |
| 128 | } |
| 129 | return getConfigJsonPath(); |
| 130 | } |
| 131 | |
| 132 | export function getConfigTsPath(): string { |
| 133 | const p = path.join(getContinueGlobalPath(), "config.ts"); |
| 134 | if (!fs.existsSync(p)) { |
| 135 | fs.writeFileSync(p, DEFAULT_CONFIG_TS_CONTENTS); |
| 136 | } |
| 137 | |
| 138 | const typesPath = path.join(getContinueGlobalPath(), "types"); |
| 139 | if (!fs.existsSync(typesPath)) { |
| 140 | fs.mkdirSync(typesPath); |
| 141 | } |
| 142 | const corePath = path.join(typesPath, "core"); |
| 143 | if (!fs.existsSync(corePath)) { |
| 144 | fs.mkdirSync(corePath); |
| 145 | } |
| 146 | const packageJsonPath = path.join(getContinueGlobalPath(), "package.json"); |
| 147 | if (!fs.existsSync(packageJsonPath)) { |
| 148 | fs.writeFileSync( |
| 149 | packageJsonPath, |
| 150 | JSON.stringify({ |
| 151 | name: "continue-config", |
| 152 | version: "1.0.0", |
| 153 | description: "My Continue Configuration", |
| 154 | main: "config.js", |
| 155 | }), |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | fs.writeFileSync(path.join(corePath, "index.d.ts"), Types); |
| 160 | return p; |
| 161 | } |
| 162 | |
| 163 | export function getConfigJsPath(): string { |
| 164 | // Do not create automatically |
| 165 | return path.join(getContinueGlobalPath(), "out", "config.js"); |
| 166 | } |
| 167 | |
| 168 | export function getTsConfigPath(): string { |
| 169 | const tsConfigPath = path.join(getContinueGlobalPath(), "tsconfig.json"); |
| 170 | if (!fs.existsSync(tsConfigPath)) { |
| 171 | fs.writeFileSync( |
| 172 | tsConfigPath, |
| 173 | JSON.stringify( |
| 174 | { |
| 175 | compilerOptions: { |
| 176 | target: "ESNext", |
| 177 | useDefineForClassFields: true, |
| 178 | lib: ["DOM", "DOM.Iterable", "ESNext"], |
| 179 | allowJs: true, |
| 180 | skipLibCheck: true, |
| 181 | esModuleInterop: false, |
| 182 | allowSyntheticDefaultImports: true, |
| 183 | strict: true, |
| 184 | forceConsistentCasingInFileNames: true, |
| 185 | module: "System", |
| 186 | moduleResolution: "Node", |
| 187 | noEmit: false, |
| 188 | noEmitOnError: false, |
| 189 | outFile: "./out/config.js", |
| 190 | typeRoots: ["./node_modules/@types", "./types"], |
| 191 | }, |
| 192 | include: ["./config.ts"], |
| 193 | }, |
| 194 | null, |
| 195 | 2, |
| 196 | ), |
| 197 | ); |
| 198 | } |
| 199 | return tsConfigPath; |
| 200 | } |
| 201 | |
| 202 | export function getContinueRcPath(): string { |
| 203 | // Disable indexing of the config folder to prevent infinite loops |
| 204 | const continuercPath = path.join(getContinueGlobalPath(), ".continuerc.json"); |
| 205 | if (!fs.existsSync(continuercPath)) { |
| 206 | fs.writeFileSync( |
| 207 | continuercPath, |
| 208 | JSON.stringify( |
| 209 | { |
| 210 | disableIndexing: true, |
| 211 | }, |
| 212 | null, |
| 213 | 2, |
| 214 | ), |
| 215 | ); |
| 216 | } |
| 217 | return continuercPath; |
| 218 | } |
| 219 | |
| 220 | function getDevDataPath(): string { |
| 221 | const sPath = path.join(getContinueGlobalPath(), "dev_data"); |
| 222 | if (!fs.existsSync(sPath)) { |
| 223 | fs.mkdirSync(sPath); |
| 224 | } |
| 225 | return sPath; |
| 226 | } |
| 227 | |
| 228 | export function getDevDataSqlitePath(): string { |
| 229 | return path.join(getDevDataPath(), "devdata.sqlite"); |
| 230 | } |
| 231 | |
| 232 | export function getDevDataFilePath( |
| 233 | eventName: DevEventName, |
| 234 | schema: string, |
| 235 | ): string { |
| 236 | const versionPath = path.join(getDevDataPath(), schema); |
| 237 | if (!fs.existsSync(versionPath)) { |
| 238 | fs.mkdirSync(versionPath); |
| 239 | } |
| 240 | return path.join(versionPath, `${String(eventName)}.jsonl`); |
| 241 | } |
| 242 | |
| 243 | function editConfigJson( |
| 244 | callback: (config: SerializedContinueConfig) => SerializedContinueConfig, |
| 245 | ): void { |
| 246 | const config = fs.readFileSync(getConfigJsonPath(), "utf8"); |
| 247 | let configJson = JSONC.parse(config); |
| 248 | // Check if it's an object |
| 249 | if (typeof configJson === "object" && configJson !== null) { |
| 250 | configJson = callback(configJson as any) as any; |
| 251 | fs.writeFileSync(getConfigJsonPath(), JSONC.stringify(configJson, null, 2)); |
| 252 | } else { |
| 253 | console.warn("config.json is not a valid object"); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | function editConfigYaml(callback: (config: ConfigYaml) => ConfigYaml): void { |
| 258 | const config = fs.readFileSync(getConfigYamlPath(), "utf8"); |
| 259 | let configYaml = YAML.parse(config); |
| 260 | // Check if it's an object |
| 261 | if (typeof configYaml === "object" && configYaml !== null) { |
| 262 | configYaml = callback(configYaml as any) as any; |
| 263 | fs.writeFileSync(getConfigYamlPath(), YAML.stringify(configYaml)); |
| 264 | } else { |
| 265 | console.warn("config.yaml is not a valid object"); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | export function editConfigFile( |
| 270 | configJsonCallback: ( |
| 271 | config: SerializedContinueConfig, |
| 272 | ) => SerializedContinueConfig, |
| 273 | configYamlCallback: (config: ConfigYaml) => ConfigYaml, |
| 274 | ): void { |
| 275 | if (fs.existsSync(getConfigYamlPath())) { |
| 276 | editConfigYaml(configYamlCallback); |
| 277 | } else if (fs.existsSync(getConfigJsonPath())) { |
| 278 | editConfigJson(configJsonCallback); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | function getMigrationsFolderPath(): string { |
| 283 | const migrationsPath = path.join(getContinueGlobalPath(), ".migrations"); |
| 284 | if (!fs.existsSync(migrationsPath)) { |
| 285 | fs.mkdirSync(migrationsPath); |
| 286 | } |
| 287 | return migrationsPath; |
| 288 | } |
| 289 | |
| 290 | export async function migrate( |
| 291 | id: string, |
| 292 | callback: () => void | Promise<void>, |
| 293 | onAlreadyComplete?: () => void, |
| 294 | ) { |
| 295 | if (process.env.NODE_ENV === "test") { |
| 296 | return await Promise.resolve(callback()); |
| 297 | } |
| 298 | |
| 299 | const migrationsPath = getMigrationsFolderPath(); |
| 300 | const migrationPath = path.join(migrationsPath, id); |
| 301 | |
| 302 | if (!fs.existsSync(migrationPath)) { |
| 303 | try { |
| 304 | console.log(`Running migration: ${id}`); |
| 305 | |
| 306 | fs.writeFileSync(migrationPath, ""); |
| 307 | await Promise.resolve(callback()); |
| 308 | } catch (e) { |
| 309 | console.warn(`Migration ${id} failed`, e); |
| 310 | } |
| 311 | } else if (onAlreadyComplete) { |
| 312 | onAlreadyComplete(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | export function getIndexSqlitePath(): string { |
| 317 | return path.join(getIndexFolderPath(), "index.sqlite"); |
| 318 | } |
| 319 | |
| 320 | export function getLanceDbPath(): string { |
| 321 | return path.join(getIndexFolderPath(), "lancedb"); |
| 322 | } |
| 323 | |
| 324 | export function getTabAutocompleteCacheSqlitePath(): string { |
| 325 | return path.join(getIndexFolderPath(), "autocompleteCache.sqlite"); |
| 326 | } |
| 327 | |
| 328 | export function getDocsSqlitePath(): string { |
| 329 | return path.join(getIndexFolderPath(), "docs.sqlite"); |
| 330 | } |
| 331 | |
| 332 | export function getRemoteConfigsFolderPath(): string { |
| 333 | const dir = path.join(getContinueGlobalPath(), ".configs"); |
| 334 | if (!fs.existsSync(dir)) { |
| 335 | fs.mkdirSync(dir); |
| 336 | } |
| 337 | return dir; |
| 338 | } |
| 339 | |
| 340 | export function getPathToRemoteConfig(remoteConfigServerUrl: string): string { |
| 341 | let url: URL | undefined = undefined; |
| 342 | try { |
| 343 | url = |
| 344 | typeof remoteConfigServerUrl !== "string" || remoteConfigServerUrl === "" |
| 345 | ? undefined |
| 346 | : new URL(remoteConfigServerUrl); |
| 347 | } catch (e) {} |
| 348 | const dir = path.join(getRemoteConfigsFolderPath(), url?.hostname ?? "None"); |
| 349 | if (!fs.existsSync(dir)) { |
| 350 | fs.mkdirSync(dir); |
| 351 | } |
| 352 | return dir; |
| 353 | } |
| 354 | |
| 355 | export function getConfigJsonPathForRemote( |
| 356 | remoteConfigServerUrl: string, |
| 357 | ): string { |
| 358 | return path.join(getPathToRemoteConfig(remoteConfigServerUrl), "config.json"); |
| 359 | } |
| 360 | |
| 361 | export function getConfigJsPathForRemote( |
| 362 | remoteConfigServerUrl: string, |
| 363 | ): string { |
| 364 | return path.join(getPathToRemoteConfig(remoteConfigServerUrl), "config.js"); |
| 365 | } |
| 366 | |
| 367 | export function getContinueDotEnv(): { [key: string]: string } { |
| 368 | const filepath = path.join(getContinueGlobalPath(), ".env"); |
| 369 | if (fs.existsSync(filepath)) { |
| 370 | return dotenv.parse(fs.readFileSync(filepath)); |
| 371 | } |
| 372 | return {}; |
| 373 | } |
| 374 | |
| 375 | export function getLogsDirPath(): string { |
| 376 | const logsPath = path.join(getContinueGlobalPath(), "logs"); |
| 377 | if (!fs.existsSync(logsPath)) { |
| 378 | fs.mkdirSync(logsPath); |
| 379 | } |
| 380 | return logsPath; |
| 381 | } |
| 382 | |
| 383 | export function getCoreLogsPath(): string { |
| 384 | return path.join(getLogsDirPath(), "core.log"); |
| 385 | } |
| 386 | |
| 387 | export function getPromptLogsPath(): string { |
| 388 | return path.join(getLogsDirPath(), "prompt.log"); |
| 389 | } |
| 390 | |
| 391 | export function getGlobalFolderWithName(name: string): string { |
| 392 | return path.join(getContinueGlobalPath(), name); |
| 393 | } |
| 394 | |
| 395 | export function getGlobalPromptsPath(): string { |
| 396 | return getGlobalFolderWithName("prompts"); |
| 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 | } |
| 469 | |
| 470 | export const isFileWithinFolder = ( |
| 471 | fileUri: string, |
| 472 | folderPath: string, |
| 473 | ): boolean => { |
| 474 | try { |
| 475 | if (!fileUri || !folderPath) { |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | const fileUriParsed = URI.parse(fileUri); |
| 480 | const fileScheme = fileUriParsed.scheme || "file"; |
| 481 | let filePath = fileUriParsed.path || ""; |
| 482 | filePath = decodeURIComponent(filePath); |
| 483 | |
| 484 | let folderWithScheme = folderPath; |
| 485 | if (!folderPath.includes("://")) { |
| 486 | folderWithScheme = `${fileScheme}://${folderPath.startsWith("/") ? "" : "/"}${folderPath}`; |
| 487 | } |
| 488 | const folderUriParsed = URI.parse(folderWithScheme); |
| 489 | |
| 490 | let folderPathClean = folderUriParsed.path || ""; |
| 491 | folderPathClean = decodeURIComponent(folderPathClean); |
| 492 | |
| 493 | filePath = filePath.replace(/\/$/, ""); |
| 494 | folderPathClean = folderPathClean.replace(/\/$/, ""); |
| 495 | |
| 496 | return ( |
| 497 | filePath === folderPathClean || filePath.startsWith(`${folderPathClean}/`) |
| 498 | ); |
| 499 | } catch (error) { |
| 500 | console.error("Error in isFileWithinFolder:", error); |
| 501 | return false; |
| 502 | } |
| 503 | }; |