From 36e9c6ac4dc7d7279bfd9e4c79b10185b1ceb14d Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Fri, 1 Dec 2023 19:48:10 +0700 Subject: [PATCH 1/2] Refactor Api Common [Server Side] [Console Log] - [+] refactor(common.ts): remove unnecessary console.log for [Org ID] in requestOpenai function - [+] refactor(common.ts): conditionally delete OpenAI-Organization header from response if [Org ID] is not set up in ENV --- app/api/common.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/app/api/common.ts b/app/api/common.ts index 6b0d619df..da5163f4e 100644 --- a/app/api/common.ts +++ b/app/api/common.ts @@ -30,10 +30,6 @@ export async function requestOpenai(req: NextRequest) { console.log("[Proxy] ", path); console.log("[Base Url]", baseUrl); - // this fix [Org ID] undefined in server side if not using custom point - if (serverConfig.openaiOrgId !== undefined) { - console.log("[Org ID]", serverConfig.openaiOrgId); - } const timeoutId = setTimeout( () => { @@ -103,12 +99,29 @@ export async function requestOpenai(req: NextRequest) { try { const res = await fetch(fetchUrl, fetchOptions); + // Extract the OpenAI-Organization header from the response + const openaiOrganizationHeader = res.headers.get("OpenAI-Organization"); + + // Check if serverConfig.openaiOrgId is defined + if (serverConfig.openaiOrgId !== undefined) { + // If openaiOrganizationHeader is present, log it; otherwise, log that the header is not present + console.log("[Org ID]", openaiOrganizationHeader); + } else { + console.log("[Org ID] is not set up."); + } + // to prevent browser prompt for credentials const newHeaders = new Headers(res.headers); newHeaders.delete("www-authenticate"); // to disable nginx buffering newHeaders.set("X-Accel-Buffering", "no"); + // Conditionally delete the OpenAI-Organization header from the response if [Org ID] is undefined (not setup in ENV) + // Also This one is to prevent the header from being sent to the client + if (!serverConfig.openaiOrgId) { + newHeaders.delete("OpenAI-Organization"); + } + return new Response(res.body, { status: res.status, statusText: res.statusText, From 8dc868207855da0de077aca739a2d5b186127326 Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Mon, 4 Dec 2023 13:32:11 +0700 Subject: [PATCH 2/2] Fix Api Common [Server Side] - [+] fix(common.ts): improve handling of OpenAI-Organization header - Check if serverConfig.openaiOrgId is defined and not an empty string - Log the value of openaiOrganizationHeader if present, otherwise log that the header is not present - Conditionally delete the OpenAI-Organization header from the response if [Org ID] is undefined or empty (not setup in ENV) --- app/api/common.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/api/common.ts b/app/api/common.ts index da5163f4e..48ddfb5f0 100644 --- a/app/api/common.ts +++ b/app/api/common.ts @@ -102,8 +102,8 @@ export async function requestOpenai(req: NextRequest) { // Extract the OpenAI-Organization header from the response const openaiOrganizationHeader = res.headers.get("OpenAI-Organization"); - // Check if serverConfig.openaiOrgId is defined - if (serverConfig.openaiOrgId !== undefined) { + // Check if serverConfig.openaiOrgId is defined and not an empty string + if (serverConfig.openaiOrgId && serverConfig.openaiOrgId.trim() !== "") { // If openaiOrganizationHeader is present, log it; otherwise, log that the header is not present console.log("[Org ID]", openaiOrganizationHeader); } else { @@ -116,9 +116,9 @@ export async function requestOpenai(req: NextRequest) { // to disable nginx buffering newHeaders.set("X-Accel-Buffering", "no"); - // Conditionally delete the OpenAI-Organization header from the response if [Org ID] is undefined (not setup in ENV) - // Also This one is to prevent the header from being sent to the client - if (!serverConfig.openaiOrgId) { + // Conditionally delete the OpenAI-Organization header from the response if [Org ID] is undefined or empty (not setup in ENV) + // Also, this is to prevent the header from being sent to the client + if (!serverConfig.openaiOrgId || serverConfig.openaiOrgId.trim() === "") { newHeaders.delete("OpenAI-Organization"); }