Move messages.json to a persist directory and remove unused imports

This commit is contained in:
powermaker450 2024-07-25 12:43:56 -04:00
parent 0b8391a7e0
commit 63e9986384
4 changed files with 12 additions and 57 deletions

1
.gitignore vendored
View file

@ -66,3 +66,4 @@ TEST-results.xml
messages.json
dist/
.env
persist/

View file

@ -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);

View file

@ -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 {

View file

@ -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 = [];
}