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