Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MrlolDev committed Dec 16, 2023
1 parent e3b08a8 commit e8621a1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 31 deletions.
70 changes: 45 additions & 25 deletions src/bot/commands/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand All @@ -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);
Expand Down
21 changes: 18 additions & 3 deletions src/bot/utils/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/bot/utils/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e8621a1

Please sign in to comment.