From e8621a166b4c1f33f617f9c849b8f463f431cd41 Mon Sep 17 00:00:00 2001 From: MrlolDev Date: Sat, 16 Dec 2023 12:05:00 +0100 Subject: [PATCH] fix --- src/bot/commands/chat.ts | 70 ++++++++++++++++++++++------------ src/bot/utils/conversations.ts | 21 ++++++++-- src/bot/utils/settings.ts | 6 +-- 3 files changed, 66 insertions(+), 31 deletions(-) diff --git a/src/bot/commands/chat.ts b/src/bot/commands/chat.ts index b6f6918..960022a 100644 --- a/src/bot/commands/chat.ts +++ b/src/bot/commands/chat.ts @@ -18,7 +18,7 @@ import { env } from "../utils/db.js"; import { LOADING_INDICATORS } from "../../types/models/users.js"; import { CHAT_MODELS } from "../models/index.js"; import EventEmitter from "events"; -import { addMessageToConversation, getConversation, newConversation } from "../utils/conversations.js"; +import { addMessageToConversation, addMessagesToConversation, getConversation, newConversation } from "../utils/conversations.js"; import { getDefaultValues, getSettingsValue } from "../utils/settings.js"; import { chargePlan, requiredPremium } from "../utils/premium.js"; @@ -95,6 +95,20 @@ async function buildInfo( const prompt: string = options?.getString("prompt") ?? ""; const user = env.user; + + let loadingIndicatorIndex = (await getSettingsValue(user, "general:loadingIndicator")) as number | string; + if ( + !loadingIndicatorIndex || + loadingIndicatorIndex == "default" || + (typeof loadingIndicatorIndex == "string" && parseInt(loadingIndicatorIndex) >= LOADING_INDICATORS.length) + ) { + loadingIndicatorIndex = (await getDefaultValues("general:loadingIndicator")) as number; + } + const loadingIndicator = LOADING_INDICATORS[loadingIndicatorIndex as number]; + await edit({ + content: `<${loadingIndicator.emoji.animated ? "a" : ""}:${loadingIndicator.emoji.name}:${loadingIndicator.emoji.id}>`, + }); + let setting = (await getSettingsValue(user, "chat:model")) as string; if (!setting) { setting = (await getDefaultValues("chat:model")) as string; @@ -134,29 +148,13 @@ async function buildInfo( } try { const event = await model.run(bot.api, data); - if (conversation) { - await addMessageToConversation(conversation, { - role: "user", - content: prompt, - }); - } else { - conversation = await newConversation( - { - role: "user", - content: prompt, - }, - userId.toString(), - modelName, - ); - } + if (!event || !(event instanceof EventEmitter)) { return await edit({ content: "An error occurred", }); } - const loadingIndicator = LOADING_INDICATORS[Math.floor(Math.random() * LOADING_INDICATORS.length)]; - let lastUpdate = Date.now(); let done = false; event.on("data", async (data) => { @@ -169,19 +167,41 @@ async function buildInfo( // if last update was more than 1 second ago lastUpdate = Date.now(); await edit({ - content: `${data.result}<${loadingIndicator.emoji.animated ? "a" : ""}:${loadingIndicator.emoji.name}:${ - loadingIndicator.emoji.id - }>`, + content: `${data.result}<${loadingIndicator.emoji.animated ? "a" : ""}:${loadingIndicator.emoji.name}:${loadingIndicator.emoji.id + }>`, }); } } else { done = true; if (conversation) { - await addMessageToConversation(conversation, { - role: "assistant", - content: data.result, - }); + await addMessagesToConversation(conversation, [ + { + role: "user", + content: prompt, + }, + { + role: "assistant", + content: data.result, + }, + ]); + } else { + conversation = await newConversation( + [ + { + role: "user", + content: prompt, + }, + { + role: "assistant", + content: data.result, + }, + ], + + userId.toString(), + modelName, + ); } + // if last update was less than 1 second ago, wait 1 second if (lastUpdate + 1000 > Date.now()) await delay(1000); await chargePlan(data.cost, env, "chat", modelName); diff --git a/src/bot/utils/conversations.ts b/src/bot/utils/conversations.ts index ae51f46..d6b780b 100644 --- a/src/bot/utils/conversations.ts +++ b/src/bot/utils/conversations.ts @@ -41,12 +41,27 @@ export async function addMessageToConversation(conversation: Conversation, messa }; await update("conversations", conversation.id, updatedConversation); } - -export async function newConversation(message: ConversationMessage, userId: string, modelName: string) { +export async function addMessagesToConversation(conversation: Conversation, messages: ConversationMessage[]) { + const updatedConversation = { + history: { + datasetId: conversation.history.datasetId, + messages: [ + ...conversation.history.messages, + ...messages.map((x) => ({ + role: x.role, + content: x.content, + })), + ], + }, + last_update: Date.now(), + }; + await update("conversations", conversation.id, updatedConversation); +} +export async function newConversation(messages: ConversationMessage[], userId: string, modelName: string) { const newConversation = { history: { datasetId: "", - messages: [message], + messages: messages, }, last_update: Date.now(), model: modelName, diff --git a/src/bot/utils/settings.ts b/src/bot/utils/settings.ts index a8572d2..3337fcf 100644 --- a/src/bot/utils/settings.ts +++ b/src/bot/utils/settings.ts @@ -151,7 +151,7 @@ export function getDefaultValues(settingId: string) { case "general:loadingIndicator": return 3; // default loading indicator case "chat:model": - return "claude-instant"; + return "gemini"; case "chat:tone": return "neutral"; case "chat:partialMessages": @@ -191,10 +191,10 @@ export function getMetadata( return { name: "Loading Indicator", description: "Which emoji to use throughout the bot to indicating loading", - options: LOADING_INDICATORS.map((l) => ({ + options: LOADING_INDICATORS.map((l, i) => ({ name: l.name, emoji: `<${l.emoji.name}:${l.emoji.id}>`, - value: l.emoji?.id || "default", + value: i || "default", })), emoji: "🔄", enabled: true,