| 1 | import { IDE } from "../.."; |
| 2 | import { walkDir } from "../../indexing/walkDir"; |
| 3 | import { readAllGlobalPromptFiles } from "../../util/paths"; |
| 4 | import { joinPathsToUri } from "../../util/uri"; |
| 5 | import { DEFAULT_PROMPTS_FOLDER_V1 } from "../v1"; |
| 6 | |
| 7 | export const DEFAULT_PROMPTS_FOLDER_V2 = ".continue/prompts"; |
| 8 | export async function getPromptFilesFromDir( |
| 9 | ide: IDE, |
| 10 | dir: string, |
| 11 | ): Promise<{ path: string; content: string }[]> { |
| 12 | try { |
| 13 | const exists = await ide.fileExists(dir); |
| 14 | |
| 15 | if (!exists) { |
| 16 | return []; |
| 17 | } |
| 18 | |
| 19 | const uris = await walkDir(dir, ide, { |
| 20 | source: "get dir prompt files", |
| 21 | }); |
| 22 | const promptFilePaths = uris.filter((p) => p.endsWith(".prompt")); |
| 23 | const results = promptFilePaths.map(async (uri) => { |
| 24 | const content = await ide.readFile(uri); // make a try catch |
| 25 | return { path: uri, content }; |
| 26 | }); |
| 27 | return Promise.all(results); |
| 28 | } catch (e) { |
| 29 | console.error(e); |
| 30 | return []; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | export async function getAllPromptFiles( |
| 35 | ide: IDE, |
| 36 | overridePromptFolder?: string, |
| 37 | checkV1DefaultFolder: boolean = false, |
| 38 | ): Promise<{ path: string; content: string }[]> { |
| 39 | const workspaceDirs = await ide.getWorkspaceDirs(); |
| 40 | let promptFiles: { path: string; content: string }[] = []; |
| 41 | |
| 42 | let dirsToCheck = [DEFAULT_PROMPTS_FOLDER_V2]; |
| 43 | if (checkV1DefaultFolder) { |
| 44 | dirsToCheck.push(DEFAULT_PROMPTS_FOLDER_V1); |
| 45 | } |
| 46 | if (overridePromptFolder) { |
| 47 | dirsToCheck = [overridePromptFolder]; |
| 48 | } |
| 49 | |
| 50 | const fullDirs = workspaceDirs |
| 51 | .map((dir) => dirsToCheck.map((d) => joinPathsToUri(dir, d))) |
| 52 | .flat(); |
| 53 | |
| 54 | promptFiles = ( |
| 55 | await Promise.all(fullDirs.map((dir) => getPromptFilesFromDir(ide, dir))) |
| 56 | ).flat(); |
| 57 | |
| 58 | // Also read from ~/.continue/.prompts |
| 59 | promptFiles.push(...readAllGlobalPromptFiles()); |
| 60 | |
| 61 | return await Promise.all( |
| 62 | promptFiles.map(async (file) => { |
| 63 | const content = await ide.readFile(file.path); |
| 64 | return { path: file.path, content }; |
| 65 | }), |
| 66 | ); |
| 67 | } |