From 2b0f2e5f9d064bc8107414b0c2e7efe61c03cdef Mon Sep 17 00:00:00 2001 From: JingSyue Date: Sun, 10 Nov 2024 10:28:25 +0800 Subject: [PATCH 1/2] fix: built-in plugin dalle3 error #5787 --- app/api/proxy.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/api/proxy.ts b/app/api/proxy.ts index 731003aa1ea..40639fdc738 100644 --- a/app/api/proxy.ts +++ b/app/api/proxy.ts @@ -1,4 +1,5 @@ import { NextRequest, NextResponse } from "next/server"; +import { getServerSideConfig } from "@/app/config/server"; export async function handle( req: NextRequest, @@ -9,6 +10,7 @@ export async function handle( if (req.method === "OPTIONS") { return NextResponse.json({ body: "OK" }, { status: 200 }); } + const serverConfig = getServerSideConfig(); // remove path params from searchParams req.nextUrl.searchParams.delete("path"); @@ -31,6 +33,11 @@ export async function handle( return true; }), ); + // if dalle3 use openai api key + if (req.headers.get("x-base-url")?.includes("openai")) { + headers.set("Authorization", `Bearer ${serverConfig.apiKey}`); + } + const controller = new AbortController(); const fetchOptions: RequestInit = { headers, From 18a657188339ac96905f0eaadd0583fdd6001f65 Mon Sep 17 00:00:00 2001 From: JingSyue <144884872+JingSyue@users.noreply.github.com> Date: Mon, 11 Nov 2024 12:59:29 +0800 Subject: [PATCH 2/2] Update proxy.ts Update proxy.ts --- app/api/proxy.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/api/proxy.ts b/app/api/proxy.ts index 40639fdc738..b3e5e7b7b93 100644 --- a/app/api/proxy.ts +++ b/app/api/proxy.ts @@ -34,9 +34,16 @@ export async function handle( }), ); // if dalle3 use openai api key - if (req.headers.get("x-base-url")?.includes("openai")) { - headers.set("Authorization", `Bearer ${serverConfig.apiKey}`); - } + const baseUrl = req.headers.get("x-base-url"); + if (baseUrl?.includes("api.openai.com")) { + if (!serverConfig.apiKey) { + return NextResponse.json( + { error: "OpenAI API key not configured" }, + { status: 500 }, + ); + } + headers.set("Authorization", `Bearer ${serverConfig.apiKey}`); + } const controller = new AbortController(); const fetchOptions: RequestInit = {