| 1 | import ignore from "ignore"; |
| 2 | import { IDE } from ".."; |
| 3 | import { |
| 4 | DEFAULT_IGNORE_DIRS, |
| 5 | DEFAULT_IGNORE_FILETYPES, |
| 6 | } from "../indexing/ignore"; |
| 7 | import { walkDir } from "../indexing/walkDir"; |
| 8 | import { getGlobalFolderWithName } from "../util/paths"; |
| 9 | import { localPathToUri } from "../util/pathToUri"; |
| 10 | import { joinPathsToUri } from "../util/uri"; |
| 11 | |
| 12 | export const ASSISTANTS = "assistants"; |
| 13 | export const ASSISTANTS_FOLDER = `.continue/${ASSISTANTS}`; |
| 14 | |
| 15 | export function isLocalAssistantFile(uri: string): boolean { |
| 16 | if (!uri.endsWith(".yaml") && !uri.endsWith(".yml")) { |
| 17 | return false; |
| 18 | } |
| 19 | |
| 20 | const normalizedUri = uri.replace(/\\/g, "/"); |
| 21 | return normalizedUri.includes(`/${ASSISTANTS_FOLDER}/`); |
| 22 | } |
| 23 | |
| 24 | export async function listYamlFilesInDir( |
| 25 | ide: IDE, |
| 26 | dir: string, |
| 27 | ): Promise<{ path: string; content: string }[]> { |
| 28 | try { |
| 29 | const exists = await ide.fileExists(dir); |
| 30 | |
| 31 | if (!exists) { |
| 32 | return []; |
| 33 | } |
| 34 | |
| 35 | const overrideDefaultIgnores = ignore() |
| 36 | .add(DEFAULT_IGNORE_FILETYPES.filter((t) => t !== "config.yaml")) |
| 37 | .add(DEFAULT_IGNORE_DIRS); |
| 38 | |
| 39 | const uris = await walkDir(dir, ide, { |
| 40 | overrideDefaultIgnores, |
| 41 | source: "get assistant files", |
| 42 | }); |
| 43 | const assistantFilePaths = uris.filter( |
| 44 | (p) => p.endsWith(".yaml") || p.endsWith(".yml"), |
| 45 | ); |
| 46 | const results = assistantFilePaths.map(async (uri) => { |
| 47 | const content = await ide.readFile(uri); // make a try catch |
| 48 | return { path: uri, content }; |
| 49 | }); |
| 50 | return Promise.all(results); |
| 51 | } catch (e) { |
| 52 | console.error(e); |
| 53 | return []; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | export interface LoadAssistantFilesOptions { |
| 58 | includeGlobal: boolean; |
| 59 | includeWorkspace: boolean; |
| 60 | } |
| 61 | |
| 62 | export function getDotContinueSubDirs( |
| 63 | ide: IDE, |
| 64 | options: LoadAssistantFilesOptions, |
| 65 | workspaceDirs: string[], |
| 66 | subDirName: string, |
| 67 | ): string[] { |
| 68 | let fullDirs: string[] = []; |
| 69 | |
| 70 | // Workspace .continue/<subDirName> |
| 71 | if (options.includeWorkspace) { |
| 72 | fullDirs = workspaceDirs.map((dir) => |
| 73 | joinPathsToUri(dir, ".continue", subDirName), |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | // ~/.continue/<subDirName> |
| 78 | if (options.includeGlobal) { |
| 79 | fullDirs.push(localPathToUri(getGlobalFolderWithName(subDirName))); |
| 80 | } |
| 81 | |
| 82 | return fullDirs; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * This method searches in both ~/.continue and workspace .continue |
| 87 | * for all YAML files in the specified subdirctory, for example .continue/assistants or .continue/prompts |
| 88 | */ |
| 89 | export async function getAllDotContinueYamlFiles( |
| 90 | ide: IDE, |
| 91 | options: LoadAssistantFilesOptions, |
| 92 | subDirName: string, |
| 93 | ): Promise<{ path: string; content: string }[]> { |
| 94 | const workspaceDirs = await ide.getWorkspaceDirs(); |
| 95 | |
| 96 | // Get all directories to check for assistant files |
| 97 | const fullDirs = getDotContinueSubDirs( |
| 98 | ide, |
| 99 | options, |
| 100 | workspaceDirs, |
| 101 | subDirName, |
| 102 | ); |
| 103 | |
| 104 | // Get all assistant files from the directories |
| 105 | const assistantFiles = ( |
| 106 | await Promise.all(fullDirs.map((dir) => listYamlFilesInDir(ide, dir))) |
| 107 | ).flat(); |
| 108 | |
| 109 | return await Promise.all( |
| 110 | assistantFiles.map(async (file) => { |
| 111 | const content = await ide.readFile(file.path); |
| 112 | return { path: file.path, content }; |
| 113 | }), |
| 114 | ); |
| 115 | } |