From 90d457c7909caf74ac3f79fcc3fc76e0d432ba7d Mon Sep 17 00:00:00 2001 From: Stef Dunlap Date: Wed, 8 Mar 2023 22:37:05 -0500 Subject: [PATCH] Add saving --- .gitignore | 1 + index.js | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 95b863a..e2fbec6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ config.json node_modules +messages.json diff --git a/index.js b/index.js index 44fc318..50cda82 100644 --- a/index.js +++ b/index.js @@ -3,13 +3,19 @@ const irc = require('irc'); const axios = require('axios'); const config = require('./config.json'); +const fs = require('fs'); +const seed_messages = [{role: 'system', content: 'You are a dungeon master, DMing a game for your friends. The setting is Left World, a dreamscape where not everything is as it seems. Let user introduce their characters and give them scenarios to interact with. Feel free to introduce NPCs, but do not make moves for the characters. If user introduces another character, keep the story going where you left off as if that new character just arrived.'}]; // context is a list of strings that are used to seed the chatgpt api and it's responses class Context { - messages = []; + currentLine = ''; currentResponse = ''; + constructor(messages = default_messages) { + this.messages = messages; + } + add_user_prompt(message) { this.messages.push({ role: 'user', content: message }); } @@ -34,9 +40,16 @@ class Context { const theLine = this.currentLine; this.currentResponse = ''; this.currentLine = ''; + this.save_history(); return theLine; } + save_history() { + const prettyData = JSON.stringify(this.messages, null, 2); + + fs.writeFileSync('./messages.json', prettyData); + } + is_response_in_progress() { return this.currentResponse !== '' || this.currentLine !== ''; } @@ -52,7 +65,8 @@ class Context { } } -const context = new Context(); +const savedMessages = require('./messages.json'); +const context = new Context(savedMessages); const client = new irc.Client(config.server, config.nick, { channels: config.channels, @@ -60,13 +74,11 @@ const client = new irc.Client(config.server, config.nick, { // listen for messages that start with !chat and call the chatgpt api with a callback that prints the response line by line client.addListener('message', async (from, to, message) => { + is_chat_cmd = message.startsWith('!chat'); is_cont_cmd = message.startsWith('!cont'); if (is_chat_cmd || is_cont_cmd) { if(context.is_response_in_progress()) { return; } - if(is_chat_cmd) { - context.clear(); - } const query = message.slice(6); chatgpt(query, (line) => { client.say(to, line);