Use Gemini 2.0 to Create Agent Instructions to Refactor an Entire Codebase

Gemini 2.0 has a context window of a million tokens, and this means that you can upload hundreds of files and allow it to inspect and analyze massive refactors across entire code bases.

This lesson demonstrates concatenating an entire code base into a single file and then uploading it to AI Studio. Once within AI Studio, we ask it to find the types of tasks that it can evaluate for us. Then we select one of the tasks and ask it to generate instructions for that task. Once those instructions are complete, we then turn them over to Windsurf's Cascade agent, and we allow Windsurf's Cascade to do all of the work for us as we sit back, relax, and let the AI do our jobs.

Share with a coworker

Transcript

[00:00] This script will take a path, use globby to grab all of the TypeScript and TSX files, then loop through them, read their contents, and then append the file name followed by the contents. So once we run this script with node, we'll glob, and we'll glob this path to our course builder, apps, egghead, source, hit enter. In this directory we now have a file which has all of the TypeScript and TSX files concatenated and they're separated by the file name. So with this file we can now open up AI Studio and simply drag and drop the file into the input box. And this was 230, 000 tokens.

[00:38] And just for reference I'll log out how many files that actually is. And you can see that that's 202 files all contained in this single file. So with extremely rough math you're probably limited to about 900 files based on a wide range of variables. Now we'll ask Gemini find five areas for improvement in this code base focusing on refactoring out duplication or patterns or simple overall cleanliness. Don't show me any solutions yet, just offer the list of five things we can investigate.

[01:07] We'll hit command-enter here and let it run, and about 45 seconds later it starts printing out text, and then after 62 seconds this completed. So looking through our options I'm going to pick option number four, probably be the simplest to demo in a video. Create explicit instructions for number four, identifying these specific files and the specific strings that can be extracted, suggest any specific files you want created, and give us exact instructions that we can hand over to a developer to clean up what you identified in issue number four. We'll paste this in, then after 88 seconds this completed and I'm gonna grab everything it printed out so you can see all of it here. And you can copy the rendered or the markdown.

[01:47] I'll take the markdown and I'll swap over to our code base that we analyzed. In WinSurf I'll open the Cascade agent, paste in the instructions, give it write permissions, and then just let it go ham and approve things along the way. Now of course this won't be perfect, but it can definitely just turn out a bunch of work that would have been manual otherwise. All right, that took about a minute and a half. I'll accept all.

[02:08] You can see it changed quite a few files. So let's open our diffs to explore them. So we'll check ability. Looks like that's importing from lib types. So this looks like the main file it created with all the different types and that all of these now import that lib types.

[02:23] And not everything is perfect. It looks like there's a conflict there. But overall this was quite a bit of work that was achieved in under 10 minutes.