Once your script can understand a user's command and identify a target file, the next step is to perform an action. This lesson shows you how to use the Vercel AI SDK's generateText function to process a file's content and produce a relevant, AI-generated response.
You'll learn how to read the content from the file path you've already identified, combine it with the inferred command into a new, more detailed prompt, and then pass it back to your local AI model to generate a final text output, such as a summary or a review.
Workflow demonstrated in this lesson:
generateText function from the ai SDK.filePath returned by generateObject.generateText function.This lesson completes the core cycle of your AI script: understanding a command, reading the relevant context, and generating a useful, text-based result, all on your local machine.
[00:00] So now we can grab generate text from the AI SDK and with our command we'll scroll down and we'll say await generate text. We'll pass in our model again and then our prompt we'll assign to a text prompt up here and while we're at it I'm going to rename this to object prompt and tab complete both of those just to avoid confusion and tab down here and assign this one to text prompt. So for our text prompt we're going to build out a command with object command and then the content and then close off the content and then we'll read in the content of the selected file. So off of file path I'm going to hit tab to automatically import read file. UTF-8 will convert it to text and then we can drop the content inside of our prompt.
[00:50] So with all of that in place we can grab the text off of the results and then log out our text. So if we run this again we'll type bun index and summarize our TypeScript config, hit enter, and then we get a full summary of everything within our TypeScript config. It explains it defines a set of compiler options for TypeScript. So let's try another prompt here with index review my package file, hit enter, and then it gives a basic review of everything defined in the package file.