A command is useless without knowing what to act upon. This lesson elevates your local AI script by giving it context from your project's file system. You'll learn how to find all relevant files and empower the AI to intelligently select the correct one based on your prompt.
The core technique involves using the globby package to scan for files and then feeding that list into a Zod enum. This constrains the AI's choices to a valid set of files, allowing it to accurately determine which file you're referring to in a natural language command.
Workflow demonstrated in this lesson:
globby library to find files based on a pattern (e.g., all .json files).filePath field to your Zod schema.filePath as z.enum(files), where files is the array of file paths returned by globby.tsconfig.json from the list of available JSON files and returns it as structured data.This lesson is a crucial step toward building powerful, context-aware tools that can intelligently interact with your project's files based on simple, human-readable commands.
[00:00] Now we're going to install a library called globby. There are plenty of libraries for globbing files. This is just the one I'm most familiar with. So import globby and grab the globby function. So then from my current directory I'm going to tell globby to find all JSON files.
[00:17] And if I just run this right now and allow this to exit, so I'll say bun index, you'll see this found all JSON files all the way from package JSON all the way into all the node modules. And if we pass in some options we'll pass in git ignore true, and this time once we run it you'll see we get our two JSON files here. And these are the full paths to the files right now they're just at the project root. So I'll remove this so this can continue And then I'm going to add to our schema a file path and we'll say this is a string and I'll just tab autocomplete a file to process. And we're going to add a feature here right after Z called enum and pass in the array of files.
[01:00] And since these types don't match because enum is expecting an array with at least one string, I'm going to go ahead and copy and paste in a type guard right after here where isNoneEmpty will check if this array has a length. Then it will be cast to this. If not, the process will exit and that will fix our little TypeScript issue. So now if I run this and say summarize our TypeScript config, hit enter, you'll see it logged out all of our files And the command is summarize and the file path is this tsconfig because it was able to select from each of these files based on the sentence.