Elffuss BraidHub

kikocis/continue public

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