|
| 1 | +/** |
| 2 | + * Stack Detector for REPL Context Enrichment |
| 3 | + * |
| 4 | + * Detects project technology stack at REPL startup to enrich LLM context. |
| 5 | + * Prevents COCO from suggesting incompatible technologies (e.g., npm in Java projects). |
| 6 | + */ |
| 7 | + |
| 8 | +import path from "node:path"; |
| 9 | +import fs from "node:fs/promises"; |
| 10 | +import { fileExists } from "../../../utils/files.js"; |
| 11 | + |
| 12 | +export type ProjectStack = "node" | "java" | "python" | "go" | "rust" | "unknown"; |
| 13 | + |
| 14 | +export interface ProjectStackContext { |
| 15 | + /** Primary language/runtime */ |
| 16 | + stack: ProjectStack; |
| 17 | + /** Package manager (npm, pnpm, yarn, maven, gradle, cargo, pip, go) */ |
| 18 | + packageManager: string | null; |
| 19 | + /** Key dependencies (name → version) */ |
| 20 | + dependencies: Record<string, string>; |
| 21 | + /** Inferred frameworks (e.g., ["Spring Boot", "React", "FastAPI"]) */ |
| 22 | + frameworks: string[]; |
| 23 | + /** Build tools detected (e.g., ["gradle", "webpack", "vite"]) */ |
| 24 | + buildTools: string[]; |
| 25 | + /** Testing frameworks (e.g., ["junit", "vitest", "pytest"]) */ |
| 26 | + testingFrameworks: string[]; |
| 27 | + /** Languages detected (e.g., ["TypeScript", "Java", "Python"]) */ |
| 28 | + languages: string[]; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Detect project stack type based on manifest files |
| 33 | + */ |
| 34 | +async function detectStack(cwd: string): Promise<ProjectStack> { |
| 35 | + if (await fileExists(path.join(cwd, "package.json"))) return "node"; |
| 36 | + if (await fileExists(path.join(cwd, "Cargo.toml"))) return "rust"; |
| 37 | + if (await fileExists(path.join(cwd, "pyproject.toml"))) return "python"; |
| 38 | + if (await fileExists(path.join(cwd, "go.mod"))) return "go"; |
| 39 | + if (await fileExists(path.join(cwd, "pom.xml"))) return "java"; |
| 40 | + if (await fileExists(path.join(cwd, "build.gradle"))) return "java"; |
| 41 | + if (await fileExists(path.join(cwd, "build.gradle.kts"))) return "java"; |
| 42 | + return "unknown"; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Detect package manager based on lock files |
| 47 | + */ |
| 48 | +async function detectPackageManager(cwd: string, stack: ProjectStack): Promise<string | null> { |
| 49 | + if (stack === "rust") return "cargo"; |
| 50 | + if (stack === "python") return "pip"; |
| 51 | + if (stack === "go") return "go"; |
| 52 | + |
| 53 | + if (stack === "java") { |
| 54 | + if ( |
| 55 | + (await fileExists(path.join(cwd, "build.gradle"))) || |
| 56 | + (await fileExists(path.join(cwd, "build.gradle.kts"))) |
| 57 | + ) { |
| 58 | + return "gradle"; |
| 59 | + } |
| 60 | + if (await fileExists(path.join(cwd, "pom.xml"))) { |
| 61 | + return "maven"; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (stack === "node") { |
| 66 | + if (await fileExists(path.join(cwd, "pnpm-lock.yaml"))) return "pnpm"; |
| 67 | + if (await fileExists(path.join(cwd, "yarn.lock"))) return "yarn"; |
| 68 | + if (await fileExists(path.join(cwd, "bun.lockb"))) return "bun"; |
| 69 | + return "npm"; |
| 70 | + } |
| 71 | + |
| 72 | + return null; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Parse Node.js package.json and extract dependencies |
| 77 | + */ |
| 78 | +async function parsePackageJson(cwd: string): Promise<{ |
| 79 | + dependencies: Record<string, string>; |
| 80 | + frameworks: string[]; |
| 81 | + buildTools: string[]; |
| 82 | + testingFrameworks: string[]; |
| 83 | + languages: string[]; |
| 84 | +}> { |
| 85 | + const packageJsonPath = path.join(cwd, "package.json"); |
| 86 | + |
| 87 | + try { |
| 88 | + const content = await fs.readFile(packageJsonPath, "utf-8"); |
| 89 | + const pkg = JSON.parse(content); |
| 90 | + |
| 91 | + const allDeps = { |
| 92 | + ...pkg.dependencies, |
| 93 | + ...pkg.devDependencies, |
| 94 | + }; |
| 95 | + |
| 96 | + // Infer frameworks from dependencies |
| 97 | + const frameworks: string[] = []; |
| 98 | + if (allDeps.react) frameworks.push("React"); |
| 99 | + if (allDeps.vue) frameworks.push("Vue"); |
| 100 | + if (allDeps["@angular/core"]) frameworks.push("Angular"); |
| 101 | + if (allDeps.next) frameworks.push("Next.js"); |
| 102 | + if (allDeps.nuxt) frameworks.push("Nuxt"); |
| 103 | + if (allDeps.express) frameworks.push("Express"); |
| 104 | + if (allDeps.fastify) frameworks.push("Fastify"); |
| 105 | + if (allDeps.nestjs || allDeps["@nestjs/core"]) frameworks.push("NestJS"); |
| 106 | + |
| 107 | + // Infer build tools |
| 108 | + const buildTools: string[] = []; |
| 109 | + if (allDeps.webpack) buildTools.push("webpack"); |
| 110 | + if (allDeps.vite) buildTools.push("vite"); |
| 111 | + if (allDeps.rollup) buildTools.push("rollup"); |
| 112 | + if (allDeps.tsup) buildTools.push("tsup"); |
| 113 | + if (allDeps.esbuild) buildTools.push("esbuild"); |
| 114 | + if (pkg.scripts?.build) buildTools.push("build"); |
| 115 | + |
| 116 | + // Infer testing frameworks |
| 117 | + const testingFrameworks: string[] = []; |
| 118 | + if (allDeps.vitest) testingFrameworks.push("vitest"); |
| 119 | + if (allDeps.jest) testingFrameworks.push("jest"); |
| 120 | + if (allDeps.mocha) testingFrameworks.push("mocha"); |
| 121 | + if (allDeps.chai) testingFrameworks.push("chai"); |
| 122 | + if (allDeps["@playwright/test"]) testingFrameworks.push("playwright"); |
| 123 | + if (allDeps.cypress) testingFrameworks.push("cypress"); |
| 124 | + |
| 125 | + // Detect languages |
| 126 | + const languages: string[] = ["JavaScript"]; |
| 127 | + if (allDeps.typescript || (await fileExists(path.join(cwd, "tsconfig.json")))) { |
| 128 | + languages.push("TypeScript"); |
| 129 | + } |
| 130 | + |
| 131 | + return { |
| 132 | + dependencies: allDeps, |
| 133 | + frameworks, |
| 134 | + buildTools, |
| 135 | + testingFrameworks, |
| 136 | + languages, |
| 137 | + }; |
| 138 | + } catch { |
| 139 | + return { |
| 140 | + dependencies: {}, |
| 141 | + frameworks: [], |
| 142 | + buildTools: [], |
| 143 | + testingFrameworks: [], |
| 144 | + languages: [], |
| 145 | + }; |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * Parse Java pom.xml and extract dependencies (basic parsing) |
| 151 | + */ |
| 152 | +async function parsePomXml(cwd: string): Promise<{ |
| 153 | + dependencies: Record<string, string>; |
| 154 | + frameworks: string[]; |
| 155 | + buildTools: string[]; |
| 156 | + testingFrameworks: string[]; |
| 157 | +}> { |
| 158 | + const pomPath = path.join(cwd, "pom.xml"); |
| 159 | + |
| 160 | + try { |
| 161 | + const content = await fs.readFile(pomPath, "utf-8"); |
| 162 | + |
| 163 | + const dependencies: Record<string, string> = {}; |
| 164 | + const frameworks: string[] = []; |
| 165 | + const buildTools: string[] = ["maven"]; |
| 166 | + const testingFrameworks: string[] = []; |
| 167 | + |
| 168 | + // Simple regex-based parsing (not full XML parser) |
| 169 | + const depRegex = /<groupId>([^<]+)<\/groupId>\s*<artifactId>([^<]+)<\/artifactId>/g; |
| 170 | + let match; |
| 171 | + |
| 172 | + while ((match = depRegex.exec(content)) !== null) { |
| 173 | + const groupId = match[1]; |
| 174 | + const artifactId = match[2]; |
| 175 | + if (!groupId || !artifactId) continue; |
| 176 | + |
| 177 | + const fullName = `${groupId}:${artifactId}`; |
| 178 | + dependencies[fullName] = "unknown"; // Version parsing would require more complex logic |
| 179 | + |
| 180 | + // Infer frameworks |
| 181 | + if (artifactId.includes("spring-boot")) { |
| 182 | + if (!frameworks.includes("Spring Boot")) frameworks.push("Spring Boot"); |
| 183 | + } |
| 184 | + if (artifactId.includes("spring-webmvc") || artifactId.includes("spring-web")) { |
| 185 | + if (!frameworks.includes("Spring MVC")) frameworks.push("Spring MVC"); |
| 186 | + } |
| 187 | + if (artifactId.includes("hibernate")) { |
| 188 | + if (!frameworks.includes("Hibernate")) frameworks.push("Hibernate"); |
| 189 | + } |
| 190 | + |
| 191 | + // Infer testing frameworks |
| 192 | + if (artifactId === "junit-jupiter" || artifactId === "junit") { |
| 193 | + if (!testingFrameworks.includes("JUnit")) testingFrameworks.push("JUnit"); |
| 194 | + } |
| 195 | + if (artifactId === "mockito-core") { |
| 196 | + if (!testingFrameworks.includes("Mockito")) testingFrameworks.push("Mockito"); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + return { dependencies, frameworks, buildTools, testingFrameworks }; |
| 201 | + } catch { |
| 202 | + return { dependencies: {}, frameworks: [], buildTools: ["maven"], testingFrameworks: [] }; |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +/** |
| 207 | + * Parse Python pyproject.toml and extract dependencies (basic parsing) |
| 208 | + */ |
| 209 | +async function parsePyprojectToml(cwd: string): Promise<{ |
| 210 | + dependencies: Record<string, string>; |
| 211 | + frameworks: string[]; |
| 212 | + buildTools: string[]; |
| 213 | + testingFrameworks: string[]; |
| 214 | +}> { |
| 215 | + const pyprojectPath = path.join(cwd, "pyproject.toml"); |
| 216 | + |
| 217 | + try { |
| 218 | + const content = await fs.readFile(pyprojectPath, "utf-8"); |
| 219 | + |
| 220 | + const dependencies: Record<string, string> = {}; |
| 221 | + const frameworks: string[] = []; |
| 222 | + const buildTools: string[] = ["pip"]; |
| 223 | + const testingFrameworks: string[] = []; |
| 224 | + |
| 225 | + // Simple line-based parsing |
| 226 | + const lines = content.split("\n"); |
| 227 | + for (const line of lines) { |
| 228 | + const trimmed = line.trim(); |
| 229 | + |
| 230 | + // Parse dependencies (very basic) |
| 231 | + if (trimmed.match(/^["']?[\w-]+["']?\s*=\s*["'][\^~>=<]+[\d.]+["']/)) { |
| 232 | + const depMatch = trimmed.match(/^["']?([\w-]+)["']?\s*=\s*["']([\^~>=<]+[\d.]+)["']/); |
| 233 | + if (depMatch && depMatch[1] && depMatch[2]) { |
| 234 | + dependencies[depMatch[1]] = depMatch[2]; |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + // Infer frameworks |
| 239 | + if (trimmed.includes("fastapi")) frameworks.push("FastAPI"); |
| 240 | + if (trimmed.includes("django")) frameworks.push("Django"); |
| 241 | + if (trimmed.includes("flask")) frameworks.push("Flask"); |
| 242 | + |
| 243 | + // Infer testing frameworks |
| 244 | + if (trimmed.includes("pytest")) testingFrameworks.push("pytest"); |
| 245 | + if (trimmed.includes("unittest")) testingFrameworks.push("unittest"); |
| 246 | + } |
| 247 | + |
| 248 | + return { dependencies, frameworks, buildTools, testingFrameworks }; |
| 249 | + } catch { |
| 250 | + return { dependencies: {}, frameworks: [], buildTools: ["pip"], testingFrameworks: [] }; |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +/** |
| 255 | + * Main entry point: Detect project stack and enrich with dependencies |
| 256 | + */ |
| 257 | +export async function detectProjectStack(cwd: string): Promise<ProjectStackContext> { |
| 258 | + const stack = await detectStack(cwd); |
| 259 | + const packageManager = await detectPackageManager(cwd, stack); |
| 260 | + |
| 261 | + let dependencies: Record<string, string> = {}; |
| 262 | + let frameworks: string[] = []; |
| 263 | + let buildTools: string[] = []; |
| 264 | + let testingFrameworks: string[] = []; |
| 265 | + let languages: string[] = []; |
| 266 | + |
| 267 | + // Parse dependencies based on stack |
| 268 | + if (stack === "node") { |
| 269 | + const parsed = await parsePackageJson(cwd); |
| 270 | + dependencies = parsed.dependencies; |
| 271 | + frameworks = parsed.frameworks; |
| 272 | + buildTools = parsed.buildTools; |
| 273 | + testingFrameworks = parsed.testingFrameworks; |
| 274 | + languages = parsed.languages; |
| 275 | + } else if (stack === "java") { |
| 276 | + const parsed = await parsePomXml(cwd); |
| 277 | + dependencies = parsed.dependencies; |
| 278 | + frameworks = parsed.frameworks; |
| 279 | + buildTools = parsed.buildTools; |
| 280 | + testingFrameworks = parsed.testingFrameworks; |
| 281 | + languages = ["Java"]; |
| 282 | + } else if (stack === "python") { |
| 283 | + const parsed = await parsePyprojectToml(cwd); |
| 284 | + dependencies = parsed.dependencies; |
| 285 | + frameworks = parsed.frameworks; |
| 286 | + buildTools = parsed.buildTools; |
| 287 | + testingFrameworks = parsed.testingFrameworks; |
| 288 | + languages = ["Python"]; |
| 289 | + } else if (stack === "go") { |
| 290 | + languages = ["Go"]; |
| 291 | + buildTools = ["go"]; |
| 292 | + } else if (stack === "rust") { |
| 293 | + languages = ["Rust"]; |
| 294 | + buildTools = ["cargo"]; |
| 295 | + } |
| 296 | + |
| 297 | + return { |
| 298 | + stack, |
| 299 | + packageManager, |
| 300 | + dependencies, |
| 301 | + frameworks, |
| 302 | + buildTools, |
| 303 | + testingFrameworks, |
| 304 | + languages, |
| 305 | + }; |
| 306 | +} |
0 commit comments