Add a custom logger, move utils into a folder

This commit is contained in:
powermaker450 2024-07-25 14:45:11 -04:00
parent f4592fff96
commit bb49873961
4 changed files with 47 additions and 12 deletions

View file

@ -10,6 +10,8 @@ import {
messageOf,
formatImageMessage,
formatUserMessage,
Logger,
emp,
} from "./utils";
import {
allowedChat,
@ -25,6 +27,7 @@ import { ImagesResponse } from "openai/resources";
import chalk from "chalk";
import dotenv from "dotenv";
dotenv.config();
const logger = new Logger();
// Specific to Tailchat. The endpoint of my Tailchat server, the bot ID and Secret.
export const HOST = process.env.HOST;
@ -33,7 +36,7 @@ export const APPSECRET = process.env.SECRET;
const allVarsFilled = HOST && APPID && APPSECRET;
if (!allVarsFilled) {
console.log("Not all required variables are filled in.");
logger.warn("Not all the required environment variables are filled in.");
process.exit(1);
}
@ -41,8 +44,8 @@ if (!allVarsFilled) {
const session = new GuildData(checkFile("messages.json", "utf-8"));
session.data.toString()
? console.log("Our conversation is:", session.data)
: console.log(
? logger.log(`Our conversation is: ${session.data}`)
: logger.log(
"Looks like we're starting fresh, no previous chat history was found.",
);
@ -58,8 +61,8 @@ client.connect().then(async () => {
const SELF = await client.whoami().then((response) => {
return response.userId;
});
console.log("Our userId is:", chalk.green(SELF));
console.log("Messages from this userId are automatically ignored.");
logger.log(`Our userId is: ${emp(SELF)}`);
logger.log("Messages from this userId are automatically ignored.");
client.onMessage(async (message) => {
const canRespondInGuild =
@ -84,7 +87,7 @@ client.connect().then(async () => {
.replace("Please generate: ", "")
.replace("please generate: ", "")
.replace("without", "|");
console.log("Generating image:", chalk.green(prompt));
logger.log(`Generating image: ${emp(prompt)}`);
await client.sendMessage({
converseId: message.converseId,
@ -113,7 +116,7 @@ client.connect().then(async () => {
.replace(/(\[(.*?)\])/g, "")
.replace("{BACKEND}", HOST);
const username = await getUsername(HOST, message.author!);
console.log("Analyzing image at:", chalk.green(imageData));
logger.log(`Analyzing the image at: ${emp(imageData)}`);
await client.sendMessage({
converseId: message.converseId,
@ -150,7 +153,7 @@ client.connect().then(async () => {
JSON.stringify(session.data),
"utf8",
);
console.log("Now our conversation is", session);
logger.log(`Now our conversation is: ${session.data}`);
} else {
const username = await getUsername(HOST, message.author!);
@ -187,7 +190,7 @@ client.connect().then(async () => {
);
}
} catch (err) {
console.log("Failed", err);
logger.error(`fs write failed: ${err}`);
await client.sendMessage({
converseId: message.converseId,
@ -225,7 +228,7 @@ client.connect().then(async () => {
fs.writeFileSync("messages.json", JSON.stringify(session.data), "utf8");
} catch (err) {
console.log("Failed", err);
logger.error(`fs write failed: ${err}`);
await client.sendMessage({
converseId: message.converseId,

View file

@ -4,8 +4,8 @@ import {
ChatHistory,
ChatHistoryData,
ImageMessage,
} from "./types";
import { system } from "./assistant";
} from "../types";
import { system } from "../assistant";
import { ChatCompletion, ChatCompletionMessage } from "openai/resources";
import { stripMentionTag } from "tailchat-client-sdk";

2
src/utils/index.ts Normal file
View file

@ -0,0 +1,2 @@
export * from "./functions";
export * from "./logger";

30
src/utils/logger.ts Normal file
View file

@ -0,0 +1,30 @@
import chalk, { ChalkFunction } from "chalk";
export class Logger {
private _wrn: ChalkFunction;
private _err: ChalkFunction;
private _main: string;
constructor() {
this._wrn = chalk.yellow;
this._err = chalk.red;
this._main = chalk.bold.gray("Tailchat-Assistant:");
}
log(text: any): void {
console.log(this._main, text);
}
warn(text: any): void {
console.warn(this._main, this._wrn(text))
}
error(text: any): void {
console.error(this._main, this._err(text));
}
}
export const emp = chalk.green;
export const wrn = chalk.yellow;
export const err = chalk.red;