From 5f837a29639e0d0d1f7acb8f4ac21880f0421379 Mon Sep 17 00:00:00 2001 From: MrlolDev Date: Sat, 16 Dec 2023 13:23:44 +0100 Subject: [PATCH] fix: 2000 characters responses --- src/bot/commands/chat.ts | 47 +++++++++++++++++++++++++++++----- src/bot/utils/conversations.ts | 9 ++----- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/bot/commands/chat.ts b/src/bot/commands/chat.ts index 960022a..5dd5dd7 100644 --- a/src/bot/commands/chat.ts +++ b/src/bot/commands/chat.ts @@ -162,14 +162,30 @@ async function buildInfo( data.result = data.result.replaceAll("@everyone", "everyone").replaceAll("@here", "here"); // make a regex to replace all mentions of users or roles data.result = data.result.replaceAll(/<&\d+>/g, "role").replaceAll(/<@\d+>/g, "user"); + const characters = data.result.split(""); + if (!data.done) { if (lastUpdate + 1000 < Date.now() && !done) { // 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 - }>`, - }); + if (characters.length > 2000) { + await edit({ + content: `<${loadingIndicator.emoji.animated ? "a" : ""}:${loadingIndicator.emoji.name}:${loadingIndicator.emoji.id + }>`, + // send result as a file + files: [ + { + name: "chat.txt", + blob: new Blob([data.result], { type: "text/plain" }), + }, + ], + }); + } else { + await edit({ + content: `${data.result}<${loadingIndicator.emoji.animated ? "a" : ""}:${loadingIndicator.emoji.name}:${loadingIndicator.emoji.id + }>`, + }); + } } } else { done = true; @@ -205,9 +221,28 @@ async function buildInfo( // 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); - - await edit({ + let contentR: { + content?: string; + files?: { + name: string; + blob: Blob; + }[]; + } = { content: `${data.result}`, + }; + if (characters.length > 2000) { + contentR = { + content: "", + files: [ + { + name: "chat.txt", + blob: new Blob([data.result], { type: "text/plain" }), + }, + ], + }; + } + await edit({ + ...contentR, components: [ { type: MessageComponentTypes.ActionRow, diff --git a/src/bot/utils/conversations.ts b/src/bot/utils/conversations.ts index 4ddea20..a698bcb 100644 --- a/src/bot/utils/conversations.ts +++ b/src/bot/utils/conversations.ts @@ -64,16 +64,11 @@ export async function addMessagesToConversation(conversation: Conversation, mess const updatedConversation = { history: { datasetId: conversation.history.datasetId, - messages: [ - ...conversation.history.messages, - ...messages.map((x) => ({ - role: x.role, - content: x.content, - })), - ], + messages: [...conversation.history.messages, ...messages], }, last_update: Date.now(), }; + console.log(updatedConversation); await update("conversations", conversation.id, updatedConversation); } export async function newConversation(messages: ConversationMessage[], userId: string, modelName: string) {