From cb071a74194f656a462f53ed503ba725c31958e3 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 21 Nov 2024 00:42:09 -0800 Subject: [PATCH] package file parsers --- core/core.ts | 4 + core/indexing/docs/suggestions/index.ts | 100 +++++++++++++++++ .../suggestions/packageCrawlers/Python.ts | 33 ++++++ .../docs/suggestions/packageCrawlers/TsJs.ts | 33 ++++++ core/protocol/core.ts | 1 + core/protocol/passThrough.ts | 1 + extensions/vscode/package-lock.json | 4 +- gui/src/hooks/useSetup.ts | 3 +- manual-testing-sandbox/package.json | 102 ++++++++++++++++++ manual-testing-sandbox/requirements.txt | 10 ++ 10 files changed, 287 insertions(+), 4 deletions(-) create mode 100644 core/indexing/docs/suggestions/index.ts create mode 100644 core/indexing/docs/suggestions/packageCrawlers/Python.ts create mode 100644 core/indexing/docs/suggestions/packageCrawlers/TsJs.ts create mode 100644 manual-testing-sandbox/package.json create mode 100644 manual-testing-sandbox/requirements.txt diff --git a/core/core.ts b/core/core.ts index c6b336c7d..d080ec34b 100644 --- a/core/core.ts +++ b/core/core.ts @@ -40,6 +40,7 @@ import { TTS } from "./util/tts"; import type { ContextItemId, IDE, IndexingProgressUpdate } from "."; import type { FromCoreProtocol, ToCoreProtocol } from "./protocol"; import type { IMessenger, Message } from "./util/messenger"; +import { getAllSuggestedDocs } from "./indexing/docs/suggestions"; export class Core { // implements IMessenger @@ -737,6 +738,9 @@ export class Core { on("indexing/initStatuses", async (msg) => { return this.docsService.initStatuses(); }); + on("docs/getSuggestedDocs", async (msg) => { + getAllSuggestedDocs(this.ide); + }); // on("didChangeSelectedProfile", (msg) => { diff --git a/core/indexing/docs/suggestions/index.ts b/core/indexing/docs/suggestions/index.ts new file mode 100644 index 000000000..d3d68aca2 --- /dev/null +++ b/core/indexing/docs/suggestions/index.ts @@ -0,0 +1,100 @@ +// write me an interface PackageCrawler that contains: +// 1. property `language` to store a given language like "python" or "typescript" +// 2. has a method `getPackageFiles` which takes a list of file names and decides which ones match package/dependency files (e.g. package.json for typescript, requirements.txt for python, etc) +// 3. has a method `parsePackageFile` which returns a list of package name and version from a relevant package file, in a standardized format like semver +// 4. has a method `getDocumentationLink` to check for documentation link for a given package (e.g. GET `https://registry.npmjs.org/` and find docs field for typescript, documentation link in the package metadata for PyPi, etc.) +// Then, write typescript classes to implement this typescript interface for the languages "python" and "typescript" + +import { IDE } from "../../.."; +import { walkDir } from "../../walkDir"; +import { PythonPackageCrawler } from "./packageCrawlers/Python"; +import { TypeScriptPackageCrawler } from "./packageCrawlers/TsJs"; + +const PACKAGE_CRAWLERS = [TypeScriptPackageCrawler, PythonPackageCrawler]; + +export interface PackageCrawler { + language: string; + getPackageFiles(fileNames: string[]): string[]; + parsePackageFile(fileContent: string, filePath: string): PackageInfo[]; + getDocumentationLink(packageName: string): Promise; +} + +export type PackageInfo = { + name: string; + version: string; + foundInFilepath: string; +}; +export type PackageDocsResult = PackageInfo & + ({ error: string; link?: never } | { link: string; error?: never }); + +export async function getAllSuggestedDocs(ide: IDE) { + const workspaceDirs = await ide.getWorkspaceDirs(); + const results = await Promise.all( + workspaceDirs.map((dir) => { + return walkDir(dir, ide); + }), + ); + const allPaths = results.flat(); // TODO only get files, not dirs. Not critical for now + const allFiles = allPaths.map((path) => path.split("/").pop()!); + const packageFilesByLanguage: Record = {}; + for (const Crawler of PACKAGE_CRAWLERS) { + const crawler = new Crawler(); + const packageFilePaths = crawler.getPackageFiles(allFiles); + packageFilesByLanguage[crawler.language] = packageFilePaths; + } + + const uniqueFilePaths = Array.from( + new Set(Object.values(packageFilesByLanguage).flat()), + ); + const fileContentsArray = await Promise.all( + uniqueFilePaths.map(async (path) => { + const contents = await ide.readFile(path); + return { path, contents }; + }), + ); + const fileContents = new Map( + fileContentsArray.map(({ path, contents }) => [path, contents]), + ); + + const packagesByLanguage: Record = {}; + PACKAGE_CRAWLERS.forEach((Crawler) => { + const crawler = new Crawler(); + const packageFiles = packageFilesByLanguage[crawler.language]; + packageFiles.forEach((file) => { + const contents = fileContents.get(file); + if (!contents) { + return; + } + const packages = crawler.parsePackageFile(contents, file); + if (!packagesByLanguage[crawler.language]) { + packagesByLanguage[crawler.language] = []; + } + packagesByLanguage[crawler.language].push(...packages); + }); + }); + console.log(packagesByLanguage); + // const allPackages = await Promise.all( + // PACKAGE_CRAWLERS.map(async (Crawler) => { + // const crawler = new Crawler(); + // const packageInfos = uniqueFileContents + // .filter(({ path }) => languageToFilePaths[crawler.language].includes(path)) + // .map(({ path, contents }) => crawler.parsePackageFile(contents, path)) + // .flat(); + // return packageInfos; + // }) + // ); + // for (const Crawler of PACKAGE_CRAWLERS) { + // const crawler = new Crawler(); + // const packages = crawler.parsePackageFile() + // languageToFilePaths[crawler.language] = packageFilePaths; + // } +} + +// I want to present the user with a list of dependencies and allow them to select which ones to index (embed) documentation for. +// In order to prevent duplicate file reads, the process will be like this: +// 1. take in a list of filepaths called `filepaths` +// 2. loop an array of PackageCrawler classes to build a map of `language` (string) to `packageFilePaths` (string[]) +// 3. Get unique filepaths from `packageFilePaths` and build a map ` of filepath to file contents using an existing `readFile` function, and skipping file reads of already in the map +// Finally, +// Add a `` method to the interface and classes that returns +// Then, assemble the classes in an array, and write a function getAllSuggestedDocs that returns a map of `language` to an ar diff --git a/core/indexing/docs/suggestions/packageCrawlers/Python.ts b/core/indexing/docs/suggestions/packageCrawlers/Python.ts new file mode 100644 index 000000000..ac0699766 --- /dev/null +++ b/core/indexing/docs/suggestions/packageCrawlers/Python.ts @@ -0,0 +1,33 @@ +import { PackageCrawler, PackageDocsResult, PackageInfo } from ".."; + +export class PythonPackageCrawler implements PackageCrawler { + language = "python"; + + getPackageFiles(fileNames: string[]): string[] { + // For Python, we typically look for files like requirements.txt or Pipfile + return fileNames.filter( + (fileName) => fileName === "requirements.txt" || fileName === "Pipfile", + ); + } + + parsePackageFile(fileContent: string, filepath: string): PackageInfo[] { + // Assume the fileContent is a string from a requirements.txt formatted file + return fileContent + .split("\n") + .map((line) => { + const [name, version] = line.split("=="); + return { name, version, foundInFilepath: filepath }; + }) + .filter((pkg) => pkg.name && pkg.version); + } + + async getDocumentationLink(packageName: string): Promise { + // Fetch metadata from PyPI to find the documentation link + const response = await fetch(`https://pypi.org/pypi/${packageName}/json`); + if (!response.ok) { + throw new Error(`Could not fetch data for package ${packageName}`); + } + const data = await response.json(); + return data.info.project_urls?.Documentation; + } +} diff --git a/core/indexing/docs/suggestions/packageCrawlers/TsJs.ts b/core/indexing/docs/suggestions/packageCrawlers/TsJs.ts new file mode 100644 index 000000000..fb073e2a1 --- /dev/null +++ b/core/indexing/docs/suggestions/packageCrawlers/TsJs.ts @@ -0,0 +1,33 @@ +import { PackageCrawler, PackageDocsResult, PackageInfo } from ".."; + +export class TypeScriptPackageCrawler implements PackageCrawler { + language = "typescript"; + + getPackageFiles(fileNames: string[]): string[] { + // For TypeScript, we look for package.json file + return fileNames.filter((fileName) => fileName === "package.json"); + } + + parsePackageFile(fileContent: string, filepath: string): PackageInfo[] { + // Parse the package.json content + const jsonData = JSON.parse(fileContent) as Record; + const dependencies = Object.entries(jsonData.dependencies || {}).concat( + Object.entries(jsonData.devDependencies || {}), + ); + return dependencies.map(([name, version]) => ({ + name, + version, + foundInFilepath: filepath, + })); + } + + async getDocumentationLink(packageName: string): Promise { + // Fetch metadata from the NPM registry to find the documentation link + const response = await fetch(`https://registry.npmjs.org/${packageName}`); + if (!response.ok) { + throw new Error(`Could not fetch data for package ${packageName}`); + } + const data = await response.json(); + return data.homepage; + } +} diff --git a/core/protocol/core.ts b/core/protocol/core.ts index c757e5d34..069f9e297 100644 --- a/core/protocol/core.ts +++ b/core/protocol/core.ts @@ -167,6 +167,7 @@ export type ToCoreFromIdeOrWebviewProtocol = { "indexing/abort": [{ type: string; id: string }, void]; "indexing/setPaused": [{ type: string; id: string; paused: boolean }, void]; "indexing/initStatuses": [undefined, void]; + "docs/getSuggestedDocs": [undefined, void]; addAutocompleteModel: [{ model: ModelDescription }, void]; diff --git a/core/protocol/passThrough.ts b/core/protocol/passThrough.ts index 40d537d41..ebe2a8697 100644 --- a/core/protocol/passThrough.ts +++ b/core/protocol/passThrough.ts @@ -52,6 +52,7 @@ export const WEBVIEW_TO_CORE_PASS_THROUGH: (keyof ToCoreFromWebviewProtocol)[] = "indexing/reindex", "indexing/abort", "indexing/setPaused", + "docs/getSuggestedDocs", // "completeOnboarding", "addAutocompleteModel", diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 940503505..b30a951df 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -1,12 +1,12 @@ { "name": "continue", - "version": "0.9.232", + "version": "0.9.233", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "continue", - "version": "0.9.232", + "version": "0.9.233", "license": "Apache-2.0", "dependencies": { "@electron/rebuild": "^3.2.10", diff --git a/gui/src/hooks/useSetup.ts b/gui/src/hooks/useSetup.ts index 4bef9da9c..f18379889 100644 --- a/gui/src/hooks/useSetup.ts +++ b/gui/src/hooks/useSetup.ts @@ -84,6 +84,7 @@ function useSetup(dispatch: Dispatch) { // ON LOAD useEffect(() => { + ideMessenger.post("docs/getSuggestedDocs", undefined); // Override persisted state dispatch(setInactive()); @@ -178,8 +179,6 @@ function useSetup(dispatch: Dispatch) { }, [defaultModelTitle], ); - - } export default useSetup; diff --git a/manual-testing-sandbox/package.json b/manual-testing-sandbox/package.json new file mode 100644 index 000000000..ef77ac910 --- /dev/null +++ b/manual-testing-sandbox/package.json @@ -0,0 +1,102 @@ +{ + "name": "gui", + "private": true, + "type": "module", + "author": "Continue Dev, Inc", + "license": "Apache-2.0", + "scripts": { + "dev": "vite", + "tsc:check": "tsc -p ./ --noEmit", + "build": "tsc && vite build", + "preview": "vite preview", + "test": "vitest run", + "test:coverage": "vitest run --coverage", + "test:ui": "vitest --ui", + "test:watch": "vitest" + }, + "dependencies": { + "@headlessui/react": "^1.7.17", + "@heroicons/react": "^2.0.18", + "@monaco-editor/react": "^4.6.0", + "@reduxjs/toolkit": "^1.9.3", + "@tiptap/core": "^2.3.2", + "@tiptap/extension-document": "^2.3.2", + "@tiptap/extension-dropcursor": "^2.1.16", + "@tiptap/extension-history": "^2.3.2", + "@tiptap/extension-image": "^2.1.16", + "@tiptap/extension-mention": "^2.1.13", + "@tiptap/extension-paragraph": "^2.3.2", + "@tiptap/extension-placeholder": "^2.1.13", + "@tiptap/extension-text": "^2.3.2", + "@tiptap/pm": "^2.1.13", + "@tiptap/react": "^2.1.13", + "@tiptap/starter-kit": "^2.1.13", + "@tiptap/suggestion": "^2.1.13", + "@types/vscode-webview": "^1.57.1", + "core": "file:../core", + "dompurify": "^3.0.6", + "downshift": "^7.6.0", + "lodash": "^4.17.21", + "minisearch": "^7.0.2", + "onigasm": "^2.2.5", + "posthog-js": "^1.130.1", + "prismjs": "^1.29.0", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "react-error-boundary": "^4.0.11", + "react-hook-form": "^7.47.0", + "react-intersection-observer": "^9.13.1", + "react-markdown": "^9.0.1", + "react-redux": "^8.0.5", + "react-remark": "^2.1.0", + "react-router-dom": "^6.14.2", + "react-switch": "^7.0.0", + "react-syntax-highlighter": "^15.5.0", + "react-tooltip": "^5.18.0", + "redux-persist": "^6.0.0", + "redux-persist-transform-filter": "^0.0.22", + "rehype-highlight": "^7.0.0", + "rehype-katex": "^7.0.0", + "rehype-wrap-all": "^1.1.0", + "remark-math": "^6.0.0", + "reselect": "^5.1.1", + "seti-file-icons": "^0.0.8", + "socket.io-client": "^4.7.2", + "styled-components": "^5.3.6", + "table": "^6.8.1", + "tippy.js": "^6.3.7", + "unist-util-visit": "^5.0.0", + "uuid": "^9.0.1", + "vscode-webview": "^1.0.1-beta.1" + }, + "devDependencies": { + "@swc/cli": "^0.3.14", + "@swc/core": "^1.7.26", + "@testing-library/dom": "^10.4.0", + "@testing-library/jest-dom": "^6.5.0", + "@testing-library/react": "^16.0.1", + "@testing-library/user-event": "^14.5.2", + "@types/lodash": "^4.17.6", + "@types/node": "^20.5.6", + "@types/node-fetch": "^2.6.4", + "@types/react": "^18.3.11", + "@types/react-dom": "^18.3.1", + "@types/react-router-dom": "^5.3.3", + "@types/react-syntax-highlighter": "^15.5.7", + "@types/styled-components": "^5.1.26", + "@vitejs/plugin-react-swc": "^3.7.0", + "@vitest/coverage-v8": "^2.1.3", + "@vitest/ui": "^2.1.3", + "autoprefixer": "^10.4.13", + "jsdom": "^25.0.1", + "postcss": "^8.4.21", + "tailwindcss": "^3.2.7", + "typescript": "^4.9.3", + "vite": "^4.1.0", + "vitest": "^2.1.3" + }, + "engine-strict": true, + "engines": { + "node": ">=20.11.0" + } +} diff --git a/manual-testing-sandbox/requirements.txt b/manual-testing-sandbox/requirements.txt new file mode 100644 index 000000000..793d6a827 --- /dev/null +++ b/manual-testing-sandbox/requirements.txt @@ -0,0 +1,10 @@ +flask==2.1.1 +requests==2.28.1 +numpy==1.23.4 +pandas==1.5.0 +scipy==1.9.3 +django==4.1.3 +matplotlib==3.6.2 +pytest==7.2.0 +sqlalchemy==1.4.41 +beautifulsoup4==4.11.1 \ No newline at end of file