Loading
Conversation 14 exercises
lesson

Search and Summarize Files Across Directories

Now that we have our simple file search in place, let's go ahead and duplicate "Continue Adventure" into a new script called "Summarize Adventure".

demo

A big ch

Loading lesson

Transcript

00:00 Now that we have our simple file search in place let's go ahead and duplicate continueAdventure and we'll name this summarizeAdventure. So we'll give that a name of summarizeAdventure. So now instead of feeding the contents of the file we read into the prompt as messages, we're going to delete all of this, get rid of the loaded message stuff. We'll give this a basic summarize the following story, And this time because we're not chatting with this we're going to use the editor component here. So onInit of the editor will pass the input as the contents from up above.

00:45 We can get rid of onSubmit. And now when a token is added through newToken we'll just say editorAppendToken and we can pretty much get rid of all of the rest of the stuff for now. Get rid of the current message. We don't need the timeout. We're just cleaning stuff up here.

01:06 We don't need memory because we're not remembering anything from something the user did previously. We're just submitting this single chain call. And let's clean up any of these errors that might be hanging around from stray things, and let's see what we got. So we have a path to our stories, we search for the story that we want to find with grep, we grab the contents of the file, and we pass those contents in on a knit when the component knits, we send it to the chain to call as the input, and that will fire off and work against the prompt from the system. So if I run this, I'll search for my very short keyboard story, I'll hit submit, My editor will open.

01:47 Looks like that's not running so I must have left the history placeholder in and we don't have history. So now when I run this I'll search for my keyboard story, hit enter, you'll see it will summarize our story and push it into the editor. And then once we're done, I can get the summary back from this. And I'll go ahead and write a file. And we'll just drop this in the temp path of our summarize adventure script.

02:18 We'll drop in a date dot now and dot text and we'll write in the summary as the contents of that file. And if I extract this we'll call this summary file. Is there summary file. So then once this is done I can actually open this back in VS Code since my editor is set to VS Code. So let's run this again.

02:44 We'll select the first 1. It'll start pushing in that text. Then once I hit command-s this will save the file and it'll open it inside of VS Code. I'll hit option-z to enable the word wrap and now we can select and summarize any of those stories. 1 last thing to add in I'm gonna bring in globby this time.

03:04 Let's get rid of all these unused things. So now if I say await import globby, and if you're curious why I use these dynamic imports rather than import foo from bar, These imports are a bit more portable you can drop them inside of async functions. So it's just a preference. You can import libraries however you want. So now when I run this it'll prompt me that I need to install globby.

03:29 So let's do that. And instead of continuing I'll hit command-w to exit. So now we have globby installed. So we'll grab globby here. Now I'll get all of my files from our kenvpath temp directory and allow it to select any script project in there as long as it's a text file.

03:50 So I can get rid of that. And then I don't need to map over file names anymore, because file names is just an array of file paths. So now when I run this you'll see all of the text files from all of my temp script directories, including things like the error files when I was playing around with other scripts. And I can select any of them. Let's just find a random 1.

04:12 How about the cannot find package link chain, which is the very first thing we did. I'll hit submit, and it summarizes what we ran into. I'll hit save, and this writes a file which is open in VS Code, and now I can do whatever I want with this file, and I have a tool that can search the contents of text files across directories and write summaries and export them to OpenNVS Code.