diff --git a/.gitignore b/.gitignore index 7a3cb51..8123826 100644 --- a/.gitignore +++ b/.gitignore @@ -66,3 +66,4 @@ TEST-results.xml messages.json dist/ .env +persist/ diff --git a/src/bot.ts b/src/bot.ts index 0097818..dbd9f58 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -38,7 +38,7 @@ if (!allVarsFilled) { } // Define the initial system message for the LLM. -const session = new GuildData(checkFile("./messages.json", "utf-8")); +const session = new GuildData(checkFile("messages.json", "utf-8")); session.data.toString() ? console.log("Our conversation is:", session.data) @@ -146,7 +146,7 @@ client.connect().then(async () => { }); fs.writeFileSync( - "./messages.json", + "messages.json", JSON.stringify(session.data), "utf8", ); @@ -181,7 +181,7 @@ client.connect().then(async () => { }); fs.writeFileSync( - "./messages.json", + "messages.json", JSON.stringify(session.data), "utf8", ); @@ -223,11 +223,7 @@ client.connect().then(async () => { content: `[md]${contentOf(response)}[/md]`, }); - fs.writeFileSync( - "./messages.json", - JSON.stringify(session.data), - "utf8", - ); + fs.writeFileSync("messages.json", JSON.stringify(session.data), "utf8"); } catch (err) { console.log("Failed", err); diff --git a/src/types.ts b/src/types.ts index c5ac7f5..f2200ef 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,9 +5,7 @@ import { ChatCompletionSystemMessageParam, } from "openai/resources"; import { TailchatWsClient } from "tailchat-client-sdk"; -import { ChatMessage } from "tailchat-types"; -import { formatNewHistory, formatUserMessage, getUsername } from "./utils"; -import { HOST } from "./bot"; +import { formatNewHistory } from "./utils"; import chalk from "chalk"; export interface ImageMessage { diff --git a/src/utils.ts b/src/utils.ts index e98d3b9..fbbb8b6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,6 @@ import * as fs from "fs"; import { AnyChatCompletion, - AnyChatCompletionRole, ChatHistory, ChatHistoryData, ImageMessage, @@ -9,61 +8,22 @@ import { import { system } from "./assistant"; import { ChatCompletion, ChatCompletionMessage } from "openai/resources"; import { stripMentionTag } from "tailchat-client-sdk"; -import { ChatMessage } from "tailchat-types"; -import { HOST } from "./bot"; -import chalk from "chalk"; - -//export function checkFile( -// file: string, -// encoding: fs.EncodingOption, -// defaultContent: string, -//): Messages { -// let final: Messages; -// const generic: Messages = [ -// { -// role: "system", -// content: defaultContent, -// }, -// ]; -// -// if (fs.existsSync(file)) { -// const data = fs.readFileSync(file, encoding); -// -// final = !data.toString().trim() ? generic : JSON.parse(data.toString()); -// } else { -// fs.createWriteStream(file); -// final = generic; -// } -// -// return final; -//} export function checkFile( file: string, encoding: fs.EncodingOption, ): ChatHistory { + const dirname = "./persist/"; + const fullPath = dirname + file; let final: ChatHistory; - if (fs.existsSync(file)) { - const data = fs.readFileSync(file, encoding); + if (fs.existsSync(fullPath)) { + const data = fs.readFileSync(fullPath, encoding); final = !data.toString().trim() ? [] : JSON.parse(data.toString()); - - try { - // @ts-ignore - if (final.at(0).role) { - console.warn( - chalk.yellow( - "Your persistent storage uses the old data structure for persistent messages. These messages will be moved to a backup file and the existing file will be overwritten.", - ), - ); - fs.writeFileSync(`${file}.bak`, data); - - final = []; - } - } catch {} } else { - fs.createWriteStream(file); + fs.mkdirSync(dirname); + fs.createWriteStream(fullPath); final = []; }