|
| 1 | +import * as vscode from "vscode" |
| 2 | +import * as path from "path" |
| 3 | +import * as fs from "fs/promises" |
| 4 | + |
| 5 | +/** |
| 6 | + * Get the base path for conversation storage |
| 7 | + * If the user has configured a custom path, use the custom path |
| 8 | + * Otherwise use the default VSCode extension global storage path |
| 9 | + */ |
| 10 | +export async function getStorageBasePath(defaultPath: string): Promise<string> { |
| 11 | + // Get the user-configured custom storage path |
| 12 | + const config = vscode.workspace.getConfiguration("roo-cline") |
| 13 | + const customStoragePath = config.get<string>("customStoragePath", "") |
| 14 | + |
| 15 | + // If no custom path is set, use the default path |
| 16 | + if (!customStoragePath) { |
| 17 | + return defaultPath |
| 18 | + } |
| 19 | + |
| 20 | + try { |
| 21 | + // Ensure the custom path exists |
| 22 | + await fs.mkdir(customStoragePath, { recursive: true }) |
| 23 | + |
| 24 | + // Test if the path is writable |
| 25 | + const testFile = path.join(customStoragePath, ".write_test") |
| 26 | + await fs.writeFile(testFile, "test") |
| 27 | + await fs.rm(testFile) |
| 28 | + |
| 29 | + return customStoragePath |
| 30 | + } catch (error) { |
| 31 | + // If the path cannot be used, report the error and fall back to the default path |
| 32 | + console.error(`Custom storage path cannot be used: ${error instanceof Error ? error.message : String(error)}`) |
| 33 | + vscode.window.showErrorMessage( |
| 34 | + `Custom storage path "${customStoragePath}" cannot be used, will use default path instead`, |
| 35 | + ) |
| 36 | + return defaultPath |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Get the storage directory path for a task |
| 42 | + */ |
| 43 | +export async function getTaskDirectoryPath(globalStoragePath: string, taskId: string): Promise<string> { |
| 44 | + const basePath = await getStorageBasePath(globalStoragePath) |
| 45 | + const taskDir = path.join(basePath, "tasks", taskId) |
| 46 | + await fs.mkdir(taskDir, { recursive: true }) |
| 47 | + return taskDir |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Get the settings directory path |
| 52 | + */ |
| 53 | +export async function getSettingsDirectoryPath(globalStoragePath: string): Promise<string> { |
| 54 | + const basePath = await getStorageBasePath(globalStoragePath) |
| 55 | + const settingsDir = path.join(basePath, "settings") |
| 56 | + await fs.mkdir(settingsDir, { recursive: true }) |
| 57 | + return settingsDir |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Get the cache directory path |
| 62 | + */ |
| 63 | +export async function getCacheDirectoryPath(globalStoragePath: string): Promise<string> { |
| 64 | + const basePath = await getStorageBasePath(globalStoragePath) |
| 65 | + const cacheDir = path.join(basePath, "cache") |
| 66 | + await fs.mkdir(cacheDir, { recursive: true }) |
| 67 | + return cacheDir |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Prompt user to set a custom storage path |
| 72 | + * Display an input box allowing users to enter a custom path |
| 73 | + */ |
| 74 | +export async function promptForCustomStoragePath(): Promise<void> { |
| 75 | + const currentConfig = vscode.workspace.getConfiguration("roo-cline") |
| 76 | + const currentPath = currentConfig.get<string>("customStoragePath", "") |
| 77 | + |
| 78 | + const result = await vscode.window.showInputBox({ |
| 79 | + value: currentPath, |
| 80 | + placeHolder: "D:\\RooCodeStorage", |
| 81 | + prompt: "Enter custom conversation history storage path, leave empty to use default location", |
| 82 | + validateInput: (input) => { |
| 83 | + if (!input) { |
| 84 | + return null // Allow empty value (use default path) |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + // Simple validation of path validity |
| 89 | + path.parse(input) |
| 90 | + return null // Path format is valid |
| 91 | + } catch (e) { |
| 92 | + return "Please enter a valid path" |
| 93 | + } |
| 94 | + }, |
| 95 | + }) |
| 96 | + |
| 97 | + // If user canceled the operation, result will be undefined |
| 98 | + if (result !== undefined) { |
| 99 | + await currentConfig.update("customStoragePath", result, vscode.ConfigurationTarget.Global) |
| 100 | + |
| 101 | + if (result) { |
| 102 | + try { |
| 103 | + // Test if the path is accessible |
| 104 | + await fs.mkdir(result, { recursive: true }) |
| 105 | + vscode.window.showInformationMessage(`Custom storage path set: ${result}`) |
| 106 | + } catch (error) { |
| 107 | + vscode.window.showErrorMessage( |
| 108 | + `Cannot access path ${result}: ${error instanceof Error ? error.message : String(error)}`, |
| 109 | + ) |
| 110 | + } |
| 111 | + } else { |
| 112 | + vscode.window.showInformationMessage("Restored default storage path") |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments