Loading
Conversation 14 exercises
lesson

Loading and Previewing Text Files in a Script

Let's continue our old stories in Adventure Time by creating a "Continue Adventure" script.

Copy and paste the contents of adventure-time.js into the new continue-adventure.ts file and edit the metadata at the top of the file.

![demo](https://res.cloudinary.com/johnlindquist/image/upload/v1686

Loading lesson

Transcript

00:00 To continue from some of our old stories in Adventure Time, let's create a continueAdventure

00:07 script. And

00:09 we'll just copy and paste the contents of this into here. Remove the shortcut, call this continueAdventure, and we will continue an AI conversation. Now the goal here is to load 1 of the saved conversations from our temp directory in adventure time. So if we look in there we have those 3 which I played around with. So we need to read the files from this directory.

00:34 So if we look at this directory, let's create a stories path, and there is a Kenv path helper where we can put in temp and adventure time, And then this is the path to that directory, whether you're on Mac, Windows, or Linux.

00:50 Now you can also use the home helper.

00:52 So we have home and then put .kenv in here. This achieves the same thing

00:57 in case you want to load in something from your downloads folder or somewhere else.

01:01 So let's go ahead and read that directory with readdir, passing the stories path, and these will be our file names. And then we can

01:08 pass our file names into our arg prompt choices. We'll say choose a story and select from file names. So once I run this script you'll see the file names pop up over here. Now this doesn't give us a lot of information to work with, so we're going to map our file name to an object where the name is the file name, and then the preview can be an async function which will read the contents. So read file of path.resolve, stories path, and then comma file name.

01:41 And then this is a UTF because it's a text file. And then we'll grab the contents and return the contents. So now when we open this you should see as we select a text file it will show which story we're talking about. And to help with the formatting I'm going to wrap this in the Markdown helper. Launch the script again and now you can see each file that we could select.

02:03 You can scroll down with the trackpad, or if the input is focused you can scroll down with command down and up, whereas regular down and up select 1 of the files. So once we select a file it would jump back into the story, but

02:16 we need to load the content of the file into the prompt. So let's do that by saying this is the file name. Then we're going to grab this exact same line right here and bring it down here. And what we need to do is convert these contents into human messages and AI messages and then inject them into our prompt. I already put together a function which I'll copy and paste over here which will convert this text, I'll pass this in here, into an array of messages which has from human or AI and then all the text underneath it.

02:50 And it's really important to note here that this function is based on the output in how we're saving the conversation here. So if you decide to save your conversation in a different way you're going to have to write a different parser to convert it back. It all depends on how and where you want to store those conversations. So now that we have our messages we need to inject them in here before the messages placeholder because this is where all of our input will go once we start submitting more to the story.

03:19 So we're going to take our messages

03:21 and we're going to map over our messages. And looks like Copilot knows what I'm doing. If the message is from a human, then return the human message prompt template from the text. And if the message is from the AI, we don't want a system message prompt template. We actually want an AI message prompt template.

03:41 So we'll grab that, which we haven't used yet, paste that right there, and then we'll destructure this array, correct the syntax error by adding a comma, and now we've taken all of that text from the file, turned it into an array, and then turned those into something the prompt can read and push into the conversation.

03:59 So if we take a look at this prompt and we'll say result is a wait prompt.

04:04 It's not render it's format. And then pass in an empty history since that's required. And then I'll just toss this into an editor where the value is the result and the language is going to be json because this is a string of json.

04:21 So if we run this now and I select this story it looks like I missed something in my path and

04:28 I needed to assign the value to the final name as well. Otherwise it was returning the full object. So we'll run this again and select this file. It looks like we're also missing an input variable of input because it's expecting that input right there. So we'll set an input to just nothing

04:46 And we'll try this again. Sorry about those errors. Select the text file and you'll see

04:50 that the prompt has resulted in an array of objects with text on them.

04:53 Now if you want a little more power for debugging you can put

04:56 in the debugger statement here and opening Script Kit

05:00 you go to the account tab, you sponsor script kit on GitHub, and you'll get access to the debugger. So we can connect to the process, select a file, and this will pause on the debugger line, and you can see the result down here where the result is this text. You can go into the console and json.parse the result, and play around with the array of results, and get some more information. But the key here is that this is an array of objects with text on them. So now instead of inspecting the result we can just run this, and we'll select the story about keyboard shortcuts, which I was really excited about earlier.

05:37 Hit enter. I can say what have we talked about so far. Hit enter. And you can see it's talking about everything that's already happened and giving me those options. So now since we are continuing a story and we probably don't want to force ourselves to input anything we can say onInit() and then a wait chain.

05:59 We'll grab this bring it up here and our input will be summarize the story so far. If the last message was from the AI, re-present the options. So Now when we run the script, I'll select a story that I want to continue, I'll hit enter, and it'll start summarizing everything it did so far, and then give me the options so we

06:26 can continue with our story. I'll type 3, hit enter, and it continues on. You