diff --git a/core/commands/slash/index.ts b/core/commands/slash/index.ts index e0d507a7a..0a6fc327d 100644 --- a/core/commands/slash/index.ts +++ b/core/commands/slash/index.ts @@ -1,12 +1,13 @@ -import GenerateTerminalCommand from "./cmd.js"; -import CommentSlashCommand from "./comment.js"; -import CommitMessageCommand from "./commit.js"; -import DraftIssueCommand from "./draftIssue.js"; -import EditSlashCommand from "./edit.js"; -import HttpSlashCommand from "./http.js"; -import ReviewMessageCommand from "./review.js"; -import ShareSlashCommand from "./share.js"; -import StackOverflowSlashCommand from "./stackOverflow.js"; +import GenerateTerminalCommand from "./cmd"; +import CommentSlashCommand from "./comment"; +import CommitMessageCommand from "./commit"; +import DraftIssueCommand from "./draftIssue"; +import EditSlashCommand from "./edit"; +import HttpSlashCommand from "./http"; +import ReviewMessageCommand from "./review"; +import ShareSlashCommand from "./share"; +import StackOverflowSlashCommand from "./stackOverflow"; +import OnboardSlashCommand from "./onboard"; export default [ DraftIssueCommand, @@ -18,4 +19,5 @@ export default [ HttpSlashCommand, CommitMessageCommand, ReviewMessageCommand, + OnboardSlashCommand, ]; diff --git a/core/commands/slash/onboard.ts b/core/commands/slash/onboard.ts new file mode 100644 index 000000000..fc0e0a481 --- /dev/null +++ b/core/commands/slash/onboard.ts @@ -0,0 +1,156 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import { IDE, SlashCommand } from "../.."; +import * as fs from "fs/promises"; +import * as path from "path"; +import { stripImages } from "../../llm/images"; +import ignore from "ignore"; +import { + defaultIgnoreDir, + defaultIgnoreFile, + gitIgArrayFromFile, +} from "../../indexing/ignore"; + +const LANGUAGE_DEP_MGMT_FILENAMES = [ + "package.json", // JavaScript (Node.js) + "requirements.txt", // Python + "Gemfile", // Ruby + "pom.xml", // Java (Maven) + "build.gradle", // Java (Gradle) + "composer.json", // PHP + "Cargo.toml", // Rust + "go.mod", // Go + "packages.config", // C# (.NET) + "*.csproj", // C# (.NET Core) + "pubspec.yaml", // Dart + "Project.toml", // Julia + "mix.exs", // Elixir + "rebar.config", // Erlang + "shard.yml", // Crystal + "Package.swift", // Swift + "dependencies.gradle", // Kotlin (when using Gradle) + "Podfile", // Objective-C/Swift (CocoaPods) + "*.cabal", // Haskell + "dub.json", // D +]; + +const MAX_EXPLORE_DEPTH = 2; + +const OnboardSlashCommand: SlashCommand = { + name: "onboard", + description: "Help users familiarize themselves with the codebase", + run: async function* ({ llm, ide }) { + const [workspaceDir] = await ide.getWorkspaceDirs(); + + const context = await gatherProjectContext(workspaceDir, ide); + const prompt = createOnboardingPrompt(context); + + for await (const chunk of llm.streamChat([ + { role: "user", content: prompt }, + ])) { + yield stripImages(chunk.content); + } + }, +}; + +async function getEntriesFilteredByIgnore(dir: string, ide: IDE) { + const entries = await fs.readdir(dir, { withFileTypes: true }); + + let ig = ignore().add(defaultIgnoreDir).add(defaultIgnoreFile); + + const gitIgnorePath = path.join(dir, ".gitignore"); + + const hasIgnoreFile = await fs + .access(gitIgnorePath) + .then(() => true) + .catch(() => false); + + if (hasIgnoreFile) { + const gitIgnore = await ide.readFile(gitIgnorePath); + const igPatterns = gitIgArrayFromFile(gitIgnore); + + ig = ig.add(igPatterns); + } + + const filteredEntries = entries.filter((entry) => !ig.ignores(entry.name)); + + return filteredEntries; +} + +async function gatherProjectContext( + workspaceDir: string, + ide: IDE, +): Promise { + let context = ""; + + async function exploreDirectory(dir: string, currentDepth: number = 0) { + if (currentDepth > MAX_EXPLORE_DEPTH) { + return; + } + + const entries = await getEntriesFilteredByIgnore(dir, ide); + + for (const entry of entries) { + const fullPath = path.join(dir, entry.name); + const relativePath = path.relative(workspaceDir, fullPath); + + if (entry.isDirectory()) { + context += `\nFolder: ${relativePath}\n`; + await exploreDirectory(fullPath, currentDepth + 1); + } else { + if (entry.name.toLowerCase() === "readme.md") { + const content = await fs.readFile(fullPath, "utf-8"); + context += `README for ${relativePath}:\n${content}\n\n`; + } else if (LANGUAGE_DEP_MGMT_FILENAMES.includes(entry.name)) { + const content = await fs.readFile(fullPath, "utf-8"); + context += `${entry.name} for ${relativePath}:\n${content}\n\n`; + } + } + } + } + + await exploreDirectory(workspaceDir); + + return context; +} + +function createOnboardingPrompt(context: string): string { + return ` + As a helpful AI assistant, your task is to onboard a new developer to this project. + Use the following context about the project structure, READMEs, and dependency files to create a comprehensive overview: + + ${context} + + Please provide an overview of the project with the following guidelines: + - Determine the most important folders in the project, at most 10 + - Go through each important folder step-by-step: + - Explain what each folder does in isolation by summarzing the README or package.json file, if available + - Mention the most popular or common packages used in that folder and their roles. + - After covering individual folders, zoom out to explain at most 5 high-level insights about the project's architecture: + - How different parts of the codebase fit together. + - The overall project architecture or design patterns evident from the folder structure and dependencies. + - Provide at most 5 additional insights on the project's architecture that weren't covered in the folder-by-folder breakdown. + + 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. + + Here is an example of a valid response: + + ## Important folders + + ### /folder1 + - Description: Contains the main application logic. + - Key packages: Express.js for routing, Mongoose for database operations. + + #### /folder1/folder2 + + ## Project Architecture + - The frontend is built using React and Redux for state management. + - The backend is a Node.js application using Express.js for routing and Mongoose for database operations. + - The application follows a Model-View-Controller (MVC) architecture. + + ## Additional Insights + - The project is using a monorepo structure. + - The project uses TypeScript for type checking. + `; +} + +export default OnboardSlashCommand; diff --git a/core/indexing/ignore.ts b/core/indexing/ignore.ts index e73564e2e..643b61c32 100644 --- a/core/indexing/ignore.ts +++ b/core/indexing/ignore.ts @@ -100,3 +100,10 @@ export const defaultIgnoreDir = ignore().add(DEFAULT_IGNORE_DIRS); export const DEFAULT_IGNORE = DEFAULT_IGNORE_FILETYPES.join("\n") + "\n" + DEFAULT_IGNORE_DIRS.join("\n"); + +export function gitIgArrayFromFile(file: string) { + return file + .split(/\r?\n/) // Split on new line + .map((l) => l.trim()) // Remove whitespace + .filter((l) => !/^#|^$/.test(l)); // Remove empty lines +} diff --git a/core/indexing/walkDir.ts b/core/indexing/walkDir.ts index bc70503a8..014753daf 100644 --- a/core/indexing/walkDir.ts +++ b/core/indexing/walkDir.ts @@ -6,6 +6,7 @@ import { DEFAULT_IGNORE_FILETYPES, defaultIgnoreDir, defaultIgnoreFile, + gitIgArrayFromFile, } from "./ignore.js"; export interface WalkerOptions { @@ -46,7 +47,10 @@ class DFSWalker { // walk is a depth-first search implementation public async *walk(): AsyncGenerator { - const fixupFunc = await this.newPathFixupFunc(this.options.returnRelativePaths ? "" : this.path, this.ide); + const fixupFunc = await this.newPathFixupFunc( + this.options.returnRelativePaths ? "" : this.path, + this.ide, + ); const root: WalkContext = { walkableEntry: { relPath: "", @@ -105,14 +109,7 @@ class DFSWalker { if (ignoreFilesInDir.length === 0) { return parentIgnore; } - const patterns = ignoreFilesInDir - .map((c) => { - return c - .split(/\r?\n/) - .map((l) => l.trim()) - .filter((l) => !/^#|^$/.test(l)); - }) - .flat(); + const patterns = ignoreFilesInDir.map((c) => gitIgArrayFromFile(c)).flat(); return ignore().add(parentIgnore).add(patterns); } @@ -157,7 +154,10 @@ class DFSWalker { // returns a function which will optionally prefix a root path and fixup the paths for the appropriate OS filesystem (i.e. windows) // the reason to construct this function once is to avoid the need to call ide.pathSep() multiple times - private async newPathFixupFunc(rootPath: string, ide: IDE): Promise<(relPath: string) => string> { + private async newPathFixupFunc( + rootPath: string, + ide: IDE, + ): Promise<(relPath: string) => string> { const pathSep = await ide.pathSep(); const prefix = rootPath === "" ? "" : rootPath + pathSep; if (pathSep === "/") {