From 4416f69a9b299c03dd947e52a033af202672597d Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Thu, 13 Jun 2024 13:38:15 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20refactor:=20Use=20Undici=20Inste?= =?UTF-8?q?ad=20of=20Node-Fetch,=20prevent=20Event=20Close,=20add=20Index?= =?UTF-8?q?=20(#3052)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: Add index to conversationId field in messageSchema * refactor: prevent immediate event close on error * refactor: use undici instead of node-fetch in non-Bun environment --- api/app/clients/BaseClient.js | 6 +++++- api/models/schema/messageSchema.js | 1 + client/src/hooks/SSE/useSSE.ts | 1 - 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/api/app/clients/BaseClient.js b/api/app/clients/BaseClient.js index f2b3e7c59..0f2cd0346 100644 --- a/api/app/clients/BaseClient.js +++ b/api/app/clients/BaseClient.js @@ -1,5 +1,6 @@ const crypto = require('crypto'); -const fetch = require('node-fetch'); +const { fetch } = require('undici'); +const nodeFetch = require('node-fetch'); const { supportsBalanceCheck, Constants } = require('librechat-data-provider'); const { getConvo, getMessages, saveMessage, updateMessage, saveConvo } = require('~/models'); const { addSpaceIfNeeded, isEnabled } = require('~/server/utils'); @@ -69,6 +70,9 @@ class BaseClient { url = this.options.reverseProxyUrl; } logger.debug(`Making request to ${url}`); + if (typeof Bun !== 'undefined') { + return await nodeFetch(url, init); + } return await fetch(url, init); } diff --git a/api/models/schema/messageSchema.js b/api/models/schema/messageSchema.js index d9a6dfb93..3e89a5caa 100644 --- a/api/models/schema/messageSchema.js +++ b/api/models/schema/messageSchema.js @@ -11,6 +11,7 @@ const messageSchema = mongoose.Schema( }, conversationId: { type: String, + index: true, required: true, meiliIndex: true, }, diff --git a/client/src/hooks/SSE/useSSE.ts b/client/src/hooks/SSE/useSSE.ts index 02cc63020..8c9eae93c 100644 --- a/client/src/hooks/SSE/useSSE.ts +++ b/client/src/hooks/SSE/useSSE.ts @@ -604,7 +604,6 @@ export default function useSSE(submission: TSubmission | null, index = 0) { events.onerror = function (e: MessageEvent) { console.log('error in server stream.'); startupConfig?.checkBalance && balanceQuery.refetch(); - events.close(); let data: TResData | undefined = undefined; try {