diff --git a/api/server/controllers/agents/errors.js b/api/server/controllers/agents/errors.js index b3bb1cea65..8c8418248e 100644 --- a/api/server/controllers/agents/errors.js +++ b/api/server/controllers/agents/errors.js @@ -105,8 +105,6 @@ const createErrorHandler = ({ req, res, getContext, originPath = '/assistants/ch return res.end(); } await cache.delete(cacheKey); - // const cancelledRun = await openai.beta.threads.runs.cancel(thread_id, run_id); - // logger.debug(`[${originPath}] Cancelled run:`, cancelledRun); } catch (error) { logger.error(`[${originPath}] Error cancelling run`, error); } @@ -115,7 +113,6 @@ const createErrorHandler = ({ req, res, getContext, originPath = '/assistants/ch let run; try { - // run = await openai.beta.threads.runs.retrieve(thread_id, run_id); await recordUsage({ ...run.usage, model: run.model, @@ -128,18 +125,9 @@ const createErrorHandler = ({ req, res, getContext, originPath = '/assistants/ch let finalEvent; try { - // const errorContentPart = { - // text: { - // value: - // error?.message ?? 'There was an error processing your request. Please try again later.', - // }, - // type: ContentTypes.ERROR, - // }; - finalEvent = { final: true, conversation: await getConvo(req.user.id, conversationId), - // runMessages, }; } catch (error) { logger.error(`[${originPath}] Error finalizing error process`, error); diff --git a/api/server/controllers/assistants/chatV1.js b/api/server/controllers/assistants/chatV1.js index b4fe0d9013..09770b56d3 100644 --- a/api/server/controllers/assistants/chatV1.js +++ b/api/server/controllers/assistants/chatV1.js @@ -152,7 +152,7 @@ const chatV1 = async (req, res) => { return res.end(); } await cache.delete(cacheKey); - const cancelledRun = await openai.beta.threads.runs.cancel(thread_id, run_id); + const cancelledRun = await openai.beta.threads.runs.cancel(run_id, { thread_id }); logger.debug('[/assistants/chat/] Cancelled run:', cancelledRun); } catch (error) { logger.error('[/assistants/chat/] Error cancelling run', error); @@ -162,7 +162,7 @@ const chatV1 = async (req, res) => { let run; try { - run = await openai.beta.threads.runs.retrieve(thread_id, run_id); + run = await openai.beta.threads.runs.retrieve(run_id, { thread_id }); await recordUsage({ ...run.usage, model: run.model, @@ -623,7 +623,7 @@ const chatV1 = async (req, res) => { if (!response.run.usage) { await sleep(3000); - completedRun = await openai.beta.threads.runs.retrieve(thread_id, response.run.id); + completedRun = await openai.beta.threads.runs.retrieve(response.run.id, { thread_id }); if (completedRun.usage) { await recordUsage({ ...completedRun.usage, diff --git a/api/server/controllers/assistants/chatV2.js b/api/server/controllers/assistants/chatV2.js index e1ba93bc21..c569dc8378 100644 --- a/api/server/controllers/assistants/chatV2.js +++ b/api/server/controllers/assistants/chatV2.js @@ -467,7 +467,7 @@ const chatV2 = async (req, res) => { if (!response.run.usage) { await sleep(3000); - completedRun = await openai.beta.threads.runs.retrieve(thread_id, response.run.id); + completedRun = await openai.beta.threads.runs.retrieve(response.run.id, { thread_id }); if (completedRun.usage) { await recordUsage({ ...completedRun.usage, diff --git a/api/server/controllers/assistants/errors.js b/api/server/controllers/assistants/errors.js index 182b230fba..1c76b138c9 100644 --- a/api/server/controllers/assistants/errors.js +++ b/api/server/controllers/assistants/errors.js @@ -108,7 +108,7 @@ const createErrorHandler = ({ req, res, getContext, originPath = '/assistants/ch return res.end(); } await cache.delete(cacheKey); - const cancelledRun = await openai.beta.threads.runs.cancel(thread_id, run_id); + const cancelledRun = await openai.beta.threads.runs.cancel(run_id, { thread_id }); logger.debug(`[${originPath}] Cancelled run:`, cancelledRun); } catch (error) { logger.error(`[${originPath}] Error cancelling run`, error); @@ -118,7 +118,7 @@ const createErrorHandler = ({ req, res, getContext, originPath = '/assistants/ch let run; try { - run = await openai.beta.threads.runs.retrieve(thread_id, run_id); + run = await openai.beta.threads.runs.retrieve(run_id, { thread_id }); await recordUsage({ ...run.usage, model: run.model, diff --git a/api/server/controllers/assistants/helpers.js b/api/server/controllers/assistants/helpers.js index f5735f0b8e..1bbc0915b9 100644 --- a/api/server/controllers/assistants/helpers.js +++ b/api/server/controllers/assistants/helpers.js @@ -173,6 +173,16 @@ const listAssistantsForAzure = async ({ req, res, version, azureConfig = {}, que }; }; +/** + * Initializes the OpenAI client. + * @param {object} params - The parameters object. + * @param {ServerRequest} params.req - The request object. + * @param {ServerResponse} params.res - The response object. + * @param {TEndpointOption} params.endpointOption - The endpoint options. + * @param {boolean} params.initAppClient - Whether to initialize the app client. + * @param {string} params.overrideEndpoint - The endpoint to override. + * @returns {Promise<{ openai: OpenAIClient, openAIApiKey: string; client: import('~/app/clients/OpenAIClient') }>} - The initialized OpenAI client. + */ async function getOpenAIClient({ req, res, endpointOption, initAppClient, overrideEndpoint }) { let endpoint = overrideEndpoint ?? req.body.endpoint ?? req.query.endpoint; const version = await getCurrentVersion(req, endpoint); diff --git a/api/server/controllers/assistants/v1.js b/api/server/controllers/assistants/v1.js index e723cda4fc..10c59d9138 100644 --- a/api/server/controllers/assistants/v1.js +++ b/api/server/controllers/assistants/v1.js @@ -197,7 +197,7 @@ const deleteAssistant = async (req, res) => { await validateAuthor({ req, openai }); const assistant_id = req.params.id; - const deletionStatus = await openai.beta.assistants.del(assistant_id); + const deletionStatus = await openai.beta.assistants.delete(assistant_id); if (deletionStatus?.deleted) { await deleteAssistantActions({ req, assistant_id }); } @@ -365,7 +365,7 @@ const uploadAssistantAvatar = async (req, res) => { try { await fs.unlink(req.file.path); logger.debug('[/:agent_id/avatar] Temp. image upload file deleted'); - } catch (error) { + } catch { logger.debug('[/:agent_id/avatar] Temp. image upload file already deleted'); } } diff --git a/api/server/middleware/abortRun.js b/api/server/middleware/abortRun.js index 2846c6eefc..dd50d7bf94 100644 --- a/api/server/middleware/abortRun.js +++ b/api/server/middleware/abortRun.js @@ -47,7 +47,7 @@ async function abortRun(req, res) { try { await cache.set(cacheKey, 'cancelled', three_minutes); - const cancelledRun = await openai.beta.threads.runs.cancel(thread_id, run_id); + const cancelledRun = await openai.beta.threads.runs.cancel(run_id, { thread_id }); logger.debug('[abortRun] Cancelled run:', cancelledRun); } catch (error) { logger.error('[abortRun] Error cancelling run', error); @@ -60,7 +60,7 @@ async function abortRun(req, res) { } try { - const run = await openai.beta.threads.runs.retrieve(thread_id, run_id); + const run = await openai.beta.threads.runs.retrieve(run_id, { thread_id }); await recordUsage({ ...run.usage, model: run.model, diff --git a/api/server/routes/convos.js b/api/server/routes/convos.js index 18dbf8db0a..7113f2d63d 100644 --- a/api/server/routes/convos.js +++ b/api/server/routes/convos.js @@ -111,7 +111,7 @@ router.delete('/', async (req, res) => { /** @type {{ openai: OpenAI }} */ const { openai } = await assistantClients[endpoint].initializeClient({ req, res }); try { - const response = await openai.beta.threads.del(thread_id); + const response = await openai.beta.threads.delete(thread_id); logger.debug('Deleted OpenAI thread:', response); } catch (error) { logger.error('Error deleting OpenAI thread:', error); diff --git a/api/server/services/AssistantService.js b/api/server/services/AssistantService.js index 5354b2e33a..a9ac26e476 100644 --- a/api/server/services/AssistantService.js +++ b/api/server/services/AssistantService.js @@ -281,7 +281,7 @@ function createInProgressHandler(openai, thread_id, messages) { openai.seenCompletedMessages.add(message_id); - const message = await openai.beta.threads.messages.retrieve(thread_id, message_id); + const message = await openai.beta.threads.messages.retrieve(message_id, { thread_id }); if (!message?.content?.length) { return; } @@ -435,9 +435,11 @@ async function runAssistant({ }; }); - const outputs = await processRequiredActions(openai, actions); - - const toolRun = await openai.beta.threads.runs.submitToolOutputs(run.thread_id, run.id, outputs); + const tool_outputs = await processRequiredActions(openai, actions); + const toolRun = await openai.beta.threads.runs.submitToolOutputs(run.id, { + thread_id: run.thread_id, + tool_outputs, + }); // Recursive call with accumulated steps and messages return await runAssistant({ diff --git a/api/server/services/Endpoints/assistants/initalize.js b/api/server/services/Endpoints/assistants/initalize.js index fe37e34108..9ef5228e34 100644 --- a/api/server/services/Endpoints/assistants/initalize.js +++ b/api/server/services/Endpoints/assistants/initalize.js @@ -6,7 +6,7 @@ const { getUserKeyExpiry, checkUserKeyExpiry, } = require('~/server/services/UserService'); -const OpenAIClient = require('~/app/clients/OpenAIClient'); +const OAIClient = require('~/app/clients/OpenAIClient'); const { isUserProvided } = require('~/server/utils'); const initializeClient = async ({ req, res, endpointOption, version, initAppClient = false }) => { @@ -79,7 +79,7 @@ const initializeClient = async ({ req, res, endpointOption, version, initAppClie openai.res = res; if (endpointOption && initAppClient) { - const client = new OpenAIClient(apiKey, clientOptions); + const client = new OAIClient(apiKey, clientOptions); return { client, openai, diff --git a/api/server/services/Endpoints/azureAssistants/initialize.js b/api/server/services/Endpoints/azureAssistants/initialize.js index e002da566c..51d52b8ac2 100644 --- a/api/server/services/Endpoints/azureAssistants/initialize.js +++ b/api/server/services/Endpoints/azureAssistants/initialize.js @@ -3,11 +3,11 @@ const { ProxyAgent } = require('undici'); const { constructAzureURL, isUserProvided, resolveHeaders } = require('@librechat/api'); const { ErrorTypes, EModelEndpoint, mapModelToAzureConfig } = require('librechat-data-provider'); const { + checkUserKeyExpiry, getUserKeyValues, getUserKeyExpiry, - checkUserKeyExpiry, } = require('~/server/services/UserService'); -const OpenAIClient = require('~/app/clients/OpenAIClient'); +const OAIClient = require('~/app/clients/OpenAIClient'); class Files { constructor(client) { @@ -184,7 +184,7 @@ const initializeClient = async ({ req, res, version, endpointOption, initAppClie } if (endpointOption && initAppClient) { - const client = new OpenAIClient(apiKey, clientOptions); + const client = new OAIClient(apiKey, clientOptions); return { client, openai, diff --git a/api/server/services/Runs/RunManager.js b/api/server/services/Runs/RunManager.js index c8deeb9264..39e577fb51 100644 --- a/api/server/services/Runs/RunManager.js +++ b/api/server/services/Runs/RunManager.js @@ -91,11 +91,10 @@ class RunManager { * @param {boolean} [params.final] - The end of the run polling loop, due to `requires_action`, `cancelling`, `cancelled`, `failed`, `completed`, or `expired` statuses. */ async fetchRunSteps({ openai, thread_id, run_id, runStatus, final = false }) { - // const { data: steps, first_id, last_id, has_more } = await openai.beta.threads.runs.steps.list(thread_id, run_id); + // const { data: steps, first_id, last_id, has_more } = await openai.beta.threads.runs.steps.list(run_id, { thread_id }); const { data: _steps } = await openai.beta.threads.runs.steps.list( - thread_id, run_id, - {}, + { thread_id }, { timeout: 3000, maxRetries: 5, diff --git a/api/server/services/Runs/StreamRunManager.js b/api/server/services/Runs/StreamRunManager.js index 4f6994e0cb..b8cc57e8c8 100644 --- a/api/server/services/Runs/StreamRunManager.js +++ b/api/server/services/Runs/StreamRunManager.js @@ -573,9 +573,9 @@ class StreamRunManager { let toolRun; try { toolRun = this.openai.beta.threads.runs.submitToolOutputsStream( - run.thread_id, run.id, { + thread_id: run.thread_id, tool_outputs, stream: true, }, diff --git a/api/server/services/Runs/handle.js b/api/server/services/Runs/handle.js index dd048219bb..e49f46a692 100644 --- a/api/server/services/Runs/handle.js +++ b/api/server/services/Runs/handle.js @@ -179,7 +179,7 @@ async function waitForRun({ * @return {Promise} A promise that resolves to an array of RunStep objects. */ async function _retrieveRunSteps({ openai, thread_id, run_id }) { - const runSteps = await openai.beta.threads.runs.steps.list(thread_id, run_id); + const runSteps = await openai.beta.threads.runs.steps.list(run_id, { thread_id }); return runSteps; } diff --git a/api/server/services/Threads/manage.js b/api/server/services/Threads/manage.js index 5eace214c3..4cc1e107ed 100644 --- a/api/server/services/Threads/manage.js +++ b/api/server/services/Threads/manage.js @@ -192,7 +192,8 @@ async function addThreadMetadata({ openai, thread_id, messageId, messages }) { const promises = []; for (const message of messages) { promises.push( - openai.beta.threads.messages.update(thread_id, message.id, { + openai.beta.threads.messages.update(message.id, { + thread_id, metadata: { messageId, }, @@ -263,7 +264,8 @@ async function syncMessages({ } modifyPromises.push( - openai.beta.threads.messages.update(thread_id, apiMessage.id, { + openai.beta.threads.messages.update(apiMessage.id, { + thread_id, metadata: { messageId: dbMessage.messageId, }, @@ -413,7 +415,7 @@ async function checkMessageGaps({ }) { const promises = []; promises.push(openai.beta.threads.messages.list(thread_id, defaultOrderQuery)); - promises.push(openai.beta.threads.runs.steps.list(thread_id, run_id)); + promises.push(openai.beta.threads.runs.steps.list(run_id, { thread_id })); /** @type {[{ data: ThreadMessage[] }, { data: RunStep[] }]} */ const [response, stepsResponse] = await Promise.all(promises);