Move messages.json to a persist directory and remove unused imports
This commit is contained in:
parent
0b8391a7e0
commit
63e9986384
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -66,3 +66,4 @@ TEST-results.xml
|
||||||
messages.json
|
messages.json
|
||||||
dist/
|
dist/
|
||||||
.env
|
.env
|
||||||
|
persist/
|
||||||
|
|
12
src/bot.ts
12
src/bot.ts
|
@ -38,7 +38,7 @@ if (!allVarsFilled) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define the initial system message for the LLM.
|
// 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()
|
session.data.toString()
|
||||||
? console.log("Our conversation is:", session.data)
|
? console.log("Our conversation is:", session.data)
|
||||||
|
@ -146,7 +146,7 @@ client.connect().then(async () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
"./messages.json",
|
"messages.json",
|
||||||
JSON.stringify(session.data),
|
JSON.stringify(session.data),
|
||||||
"utf8",
|
"utf8",
|
||||||
);
|
);
|
||||||
|
@ -181,7 +181,7 @@ client.connect().then(async () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
"./messages.json",
|
"messages.json",
|
||||||
JSON.stringify(session.data),
|
JSON.stringify(session.data),
|
||||||
"utf8",
|
"utf8",
|
||||||
);
|
);
|
||||||
|
@ -223,11 +223,7 @@ client.connect().then(async () => {
|
||||||
content: `[md]${contentOf(response)}[/md]`,
|
content: `[md]${contentOf(response)}[/md]`,
|
||||||
});
|
});
|
||||||
|
|
||||||
fs.writeFileSync(
|
fs.writeFileSync("messages.json", JSON.stringify(session.data), "utf8");
|
||||||
"./messages.json",
|
|
||||||
JSON.stringify(session.data),
|
|
||||||
"utf8",
|
|
||||||
);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("Failed", err);
|
console.log("Failed", err);
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,7 @@ import {
|
||||||
ChatCompletionSystemMessageParam,
|
ChatCompletionSystemMessageParam,
|
||||||
} from "openai/resources";
|
} from "openai/resources";
|
||||||
import { TailchatWsClient } from "tailchat-client-sdk";
|
import { TailchatWsClient } from "tailchat-client-sdk";
|
||||||
import { ChatMessage } from "tailchat-types";
|
import { formatNewHistory } from "./utils";
|
||||||
import { formatNewHistory, formatUserMessage, getUsername } from "./utils";
|
|
||||||
import { HOST } from "./bot";
|
|
||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
|
|
||||||
export interface ImageMessage {
|
export interface ImageMessage {
|
||||||
|
|
52
src/utils.ts
52
src/utils.ts
|
@ -1,7 +1,6 @@
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import {
|
import {
|
||||||
AnyChatCompletion,
|
AnyChatCompletion,
|
||||||
AnyChatCompletionRole,
|
|
||||||
ChatHistory,
|
ChatHistory,
|
||||||
ChatHistoryData,
|
ChatHistoryData,
|
||||||
ImageMessage,
|
ImageMessage,
|
||||||
|
@ -9,61 +8,22 @@ import {
|
||||||
import { system } from "./assistant";
|
import { system } from "./assistant";
|
||||||
import { ChatCompletion, ChatCompletionMessage } from "openai/resources";
|
import { ChatCompletion, ChatCompletionMessage } from "openai/resources";
|
||||||
import { stripMentionTag } from "tailchat-client-sdk";
|
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(
|
export function checkFile(
|
||||||
file: string,
|
file: string,
|
||||||
encoding: fs.EncodingOption,
|
encoding: fs.EncodingOption,
|
||||||
): ChatHistory {
|
): ChatHistory {
|
||||||
|
const dirname = "./persist/";
|
||||||
|
const fullPath = dirname + file;
|
||||||
let final: ChatHistory;
|
let final: ChatHistory;
|
||||||
|
|
||||||
if (fs.existsSync(file)) {
|
if (fs.existsSync(fullPath)) {
|
||||||
const data = fs.readFileSync(file, encoding);
|
const data = fs.readFileSync(fullPath, encoding);
|
||||||
|
|
||||||
final = !data.toString().trim() ? [] : JSON.parse(data.toString());
|
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 {
|
} else {
|
||||||
fs.createWriteStream(file);
|
fs.mkdirSync(dirname);
|
||||||
|
fs.createWriteStream(fullPath);
|
||||||
final = [];
|
final = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue