46 points · by kesor · 3y ago · 26 comments
Fetch a list of all the files in your project, list of every function in a TypeScript or JavaScript file, or even get the content of a specific function, all while staying in your conversation with ChatGPT. With accessible API endpoints, you can effortlessly navigate your codebase and ask ChatGPT anything you can think of about it.
Say goodbye to the days of incessant copy-pasting and welcome a more streamlined code discussion experience .
I'd love to hear your thoughts, experiences, and suggestions for improvement. Let's discuss and evolve this tool together!
But knowing how we programmers work and how we treat security collectively, I guess I've already had the answer.
I wrote a small utility to do this (https://www.npmjs.com/package/@qnzl/lockbox) and it behaves similar to `pass` by using gpg encryption. I wrote it because I was doing a lot of programming streaming and I kept exposing secrets.
So rather than having a .profile that is:
GITHUB_TOKEN="steal this"
It's
GITHUB_TOKEN=$(lockbox product/dev/githubToken)
Then you `source` it and your secrets are in your environment without putting them into plain-text.
With this specific project, it is running on your local host, so you can sanitize the data before allowing ChatGPT queries to suck it into the "cloud".
Generally speaking, you should not have secrets and tokens in your code in the first place. This specific ChatGPT plugin respects your .gitignore files (unlike CoPilot?). So if you do have some tokens in .env.secrets, but the file is ignored, it wouldn't list that for ChatGPT to read.
But you shouldn't hold tokens in your project in the first place, those belong in the running environment, not in the code.
And nice ChatGPT written post ;) it seems to love saying things like “let’s [] and [] together!”
This seems really useful, thanks for making it.
1. Prompt: "Read project code files. Describe the flow of <whatever it is doing>? Just a step by step description of each operation taking place that this software is doing. Be as detailed as you possibly can."
2. Prompt: "Suggest improvements to this process described above, how <new feature>, how <new feature> taken into account, and add any additional edge cases that you can think of to make the process more robust and to cause it <do whatever> in a more complete way.
3. *WRITE THE TWO ABOVE ANSWERS INTO A FILE CALLED GAME_PLAN.md*
4. Prompt: "Read the file called GAME_PLAN.md and write code that implements the first improvement."
5. Profit!
The feature that can read specific functions/methods out from files only works for TypeScript (at the moment).
Minimum procedure to replicate:
- open pom.xml and readme.md
- focus on readme.md
- prompt copilot chat: "summarize some of the readmes from this project"
- Receive something like "Here is a summary of the README excerpt you provided: The project is called $PROJECTNAME and is written in Java using Maven..."
- Clear the chat
- Change focus to pom.xml
- prompt copilot chat: "summarize some of the readmes from this project"
- I'm sorry, but based on the information you provided, I cannot access any README files from the project. Can you please provide me with the location of the README files so that I can help you summarize them?
- prompt copilot chat: "The location of the README file is in the project root"
- "Thank you for letting me know. Here is a summary of the README file located in the project root: The README file provides an overview of the project, called "$PROJECTNAME". The project is written in Java using Maven..."
- can you print a tree of the project files?
- "I'm sorry, but as an AI language model, I don't have access to the file system to print a tree of the project files. However, you can use the command line to print a tree of the project files."
- The location of the files is in the project root
- "You can use the command line to print a tree of the project files."
- can you tell me anything about my overall project structure?
- I'm sorry, but based on the information you provided, I don't have access to your project structure. However, I can provide some general information about a typical Java Maven project structure.
I personally don't have a CoPilot license. But I do pay the $20/mo for ChatGPT and found that the way it can reason about code, even about a whole project, is just mind blowing.
/* Write tests for [xyz/(./xyz.ts) using [abc tests](./abc.spec.ts) as an example to reference
@ChatGPT */
Reference: https://github.com/CapsuleCat/sora-by-capsule-cat
I'll have to check out how your version pulls in the project as context.
ChatGPT can be quite corny in default mode. I recommend experimenting with prompts to make it sound more natural, and not using its responses verbatim in your communications.
Cool project though.
I've been working on similar things with GPT, but in a language agnostic way. That makes things a bit trickier, having to parse arbitrary source code languages.
I was curious how you were parsing/lexing the files to pull out functions. I asked GPT about your repo, to see if I might find some useful tools for my project. It provided the helpful summary below.
Looks like you're using typescript-estree to parse the code?
$ git clone https://github.com/kesor/chatgpt-code-plugin.git
$ cd chatgpt-code-plugin
$ aider
Using git repo: .git
Use /help to see in-chat commands.
>>> how does this package find and index all the functions?
This package finds and indexes all the functions using the following process:
1 It starts by getting a list of all the files in the project using the getFileList function in src/function-utils.ts. This function recursively traverses the directories and returns an array
of file paths.
2 For each file in the list, the getFunctionList function in src/function-utils.ts is called. This function reads the file content and parses it into an Abstract Syntax Tree (AST) using a
TypeScript parser.
3 The findFunctionsInFile function in src/function-utils.ts is then called with the AST as an argument. This function traverses the AST and identifies function declarations, function
expressions, and class methods. It collects information about each function, such as its name, start and end positions, and stores it in an array.
4 Finally, the package returns an array of objects containing information about all the functions found in the project. This array can be used to index the functions and perform further
operations, such as retrieving the function's code or analyzing its structure.
>>> does it use any pre-built packages for this? or is it all homebrew?
Yes, this package uses some pre-built packages to achieve its functionality. Here are the main ones:
1 @typescript-eslint/typescript-estree: This package is used to parse TypeScript code into an Abstract Syntax Tree (AST). It is used in the getFunctionList function to parse the file content
and generate an AST.
2 fs: This is a built-in Node.js module for working with the file system. It is used to read directories, read file content, and check file stats.
3 ignore: This package is used to filter out files and directories based on the rules specified in the .gitignore file. It is used in the getFileList function to exclude files and directories
that should not be processed.
4 path: This is a built-in Node.js module for working with file and directory paths. It is used to join and resolve paths, and to get relative paths.
These pre-built packages are combined with custom code to find and index all the functions in the project.I did also point this to a much bigger project - and asked it to explain the whole thing and do a code review with suggestions of fixes. It loaded up 8 pretty big files and told me all about them. This is not something that would have been possible with copy-pasting the files into the chat interface.
My ChatGPT plugin project is also mostly language agnostic, it can load any kind of file. I was trying to minimize the amount of tokens used, which is why adding the "read a specific function in typescript" is so useful, with this feature GPT can read very long functions and still have tokens to spare.
That would have been impossible with just pasting the file contents into a single user interaction in chat, because of token limits. But with the plugin, it seems to work (mostly). It still meets token limits for very large files, or if it starts recursing into querying functions in each file - which is why I often tell it to only read the full files themselves.