Elffuss BraidHub

kikocis/continue public

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