From 36e9c6ac4dc7d7279bfd9e4c79b10185b1ceb14d Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Fri, 1 Dec 2023 19:48:10 +0700 Subject: [PATCH] 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,