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