| 1 | import ignore from "ignore"; |
| 2 | |
| 3 | import type { FileType, IDE, SlashCommand } from "../../.."; |
| 4 | import { getGlobalContinueIgArray } from "../../../indexing/continueignore"; |
| 5 | import { DEFAULT_IGNORE, gitIgArrayFromFile } from "../../../indexing/ignore"; |
| 6 | import { renderChatMessage } from "../../../util/messageContent"; |
| 7 | import { |
| 8 | findUriInDirs, |
| 9 | getUriPathBasename, |
| 10 | joinPathsToUri, |
| 11 | } from "../../../util/uri"; |
| 12 | |
| 13 | const LANGUAGE_DEP_MGMT_FILENAMES = [ |
| 14 | "package.json", // JavaScript (Node.js) |
| 15 | "requirements.txt", // Python |
| 16 | "Gemfile", // Ruby |
| 17 | "pom.xml", // Java (Maven) |
| 18 | "build.gradle", // Java (Gradle) |
| 19 | "composer.json", // PHP |
| 20 | "Cargo.toml", // Rust |
| 21 | "go.mod", // Go |
| 22 | "packages.config", // C# (.NET) |
| 23 | "*.csproj", // C# (.NET Core) |
| 24 | "pubspec.yaml", // Dart |
| 25 | "Project.toml", // Julia |
| 26 | "mix.exs", // Elixir |
| 27 | "rebar.config", // Erlang |
| 28 | "shard.yml", // Crystal |
| 29 | "Package.swift", // Swift |
| 30 | "dependencies.gradle", // Kotlin (when using Gradle) |
| 31 | "Podfile", // Objective-C/Swift (CocoaPods) |
| 32 | "*.cabal", // Haskell |
| 33 | "dub.json", // D |
| 34 | ]; |
| 35 | |
| 36 | const MAX_EXPLORE_DEPTH = 2; |
| 37 | |
| 38 | const OnboardSlashCommand: SlashCommand = { |
| 39 | name: "onboard", |
| 40 | description: "Familiarize yourself with the codebase", |
| 41 | run: async function* ({ llm, ide, abortController }) { |
| 42 | const [workspaceDir] = await ide.getWorkspaceDirs(); |
| 43 | |
| 44 | const context = await gatherProjectContext(workspaceDir, ide); |
| 45 | const prompt = createOnboardingPrompt(context); |
| 46 | |
| 47 | for await (const chunk of llm.streamChat( |
| 48 | [{ role: "user", content: prompt }], |
| 49 | abortController.signal, |
| 50 | )) { |
| 51 | yield renderChatMessage(chunk); |
| 52 | } |
| 53 | }, |
| 54 | }; |
| 55 | |
| 56 | async function getEntriesFilteredByIgnore(dir: string, ide: IDE) { |
| 57 | const ig = ignore().add(DEFAULT_IGNORE).add(getGlobalContinueIgArray()); |
| 58 | const entries = await ide.listDir(dir); |
| 59 | |
| 60 | const ignoreUri = joinPathsToUri(dir, ".gitignore"); |
| 61 | const fileExists = await ide.fileExists(ignoreUri); |
| 62 | |
| 63 | if (fileExists) { |
| 64 | const gitIgnore = await ide.readFile(ignoreUri); |
| 65 | const igPatterns = gitIgArrayFromFile(gitIgnore); |
| 66 | |
| 67 | ig.add(igPatterns); |
| 68 | } |
| 69 | |
| 70 | const workspaceDirs = await ide.getWorkspaceDirs(); |
| 71 | |
| 72 | const withRelativePaths = entries |
| 73 | .filter( |
| 74 | (entry) => |
| 75 | entry[1] === (1 as FileType.File) || |
| 76 | entry[1] === (2 as FileType.Directory), |
| 77 | ) |
| 78 | .map((entry) => { |
| 79 | const { relativePathOrBasename } = findUriInDirs(entry[0], workspaceDirs); |
| 80 | return { |
| 81 | uri: entry[0], |
| 82 | type: entry[1], |
| 83 | basename: getUriPathBasename(entry[0]), |
| 84 | relativePath: |
| 85 | relativePathOrBasename + |
| 86 | (entry[1] === (2 as FileType.Directory) ? "/" : ""), |
| 87 | }; |
| 88 | }); |
| 89 | |
| 90 | return withRelativePaths.filter((entry) => !ig.ignores(entry.relativePath)); |
| 91 | } |
| 92 | |
| 93 | async function gatherProjectContext( |
| 94 | workspaceDir: string, |
| 95 | ide: IDE, |
| 96 | ): Promise<string> { |
| 97 | let context = ""; |
| 98 | async function exploreDirectory(dir: string, currentDepth: number = 0) { |
| 99 | if (currentDepth > MAX_EXPLORE_DEPTH) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | const entries = await getEntriesFilteredByIgnore(dir, ide); |
| 104 | |
| 105 | for (const entry of entries) { |
| 106 | if (entry.type === (2 as FileType.Directory)) { |
| 107 | context += `\nFolder: ${entry.relativePath}\n`; |
| 108 | await exploreDirectory(entry.uri, currentDepth + 1); |
| 109 | } else { |
| 110 | if (entry.basename.toLowerCase() === "readme.md") { |
| 111 | const content = await ide.readFile(entry.uri); |
| 112 | context += `README for ${entry.relativePath}:\n${content}\n\n`; |
| 113 | } else if (LANGUAGE_DEP_MGMT_FILENAMES.includes(entry.basename)) { |
| 114 | const content = await ide.readFile(entry.uri); |
| 115 | context += `${entry.basename} for ${entry.relativePath}:\n${content}\n\n`; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | await exploreDirectory(workspaceDir); |
| 122 | |
| 123 | return context; |
| 124 | } |
| 125 | |
| 126 | function createOnboardingPrompt(context: string): string { |
| 127 | return ` |
| 128 | As a helpful AI assistant, your task is to onboard a new developer to this project. |
| 129 | Use the following context about the project structure, READMEs, and dependency files to create a comprehensive overview: |
| 130 | |
| 131 | ${context} |
| 132 | |
| 133 | Please provide an overview of the project with the following guidelines: |
| 134 | - Determine the most important folders in the project, at most 10 |
| 135 | - Go through each important folder step-by-step: |
| 136 | - Explain what each folder does in isolation by summarzing the README or package.json file, if available |
| 137 | - Mention the most popular or common packages used in that folder and their roles. |
| 138 | - After covering individual folders, zoom out to explain at most 5 high-level insights about the project's architecture: |
| 139 | - How different parts of the codebase fit together. |
| 140 | - The overall project architecture or design patterns evident from the folder structure and dependencies. |
| 141 | - Provide at most 5 additional insights on the project's architecture that weren't covered in the folder-by-folder breakdown. |
| 142 | |
| 143 | Your response should be structured, clear, and focused on giving the new developer both a detailed understanding of individual components and a high-level overview of the project as a whole. |
| 144 | |
| 145 | Here is an example of a valid response: |
| 146 | |
| 147 | ## Important folders |
| 148 | |
| 149 | ### /folder1 |
| 150 | - Description: Contains the main application logic. |
| 151 | - Key packages: Express.js for routing, Mongoose for database operations. |
| 152 | |
| 153 | #### /folder1/folder2 |
| 154 | |
| 155 | ## Project Architecture |
| 156 | - The frontend is built using React and Redux for state management. |
| 157 | - The backend is a Node.js application using Express.js for routing and Mongoose for database operations. |
| 158 | - The application follows a Model-View-Controller (MVC) architecture. |
| 159 | |
| 160 | ## Additional Insights |
| 161 | - The project is using a monorepo structure. |
| 162 | - The project uses TypeScript for type checking. |
| 163 | `; |
| 164 | } |
| 165 | |
| 166 | export default OnboardSlashCommand; |