|
| 1 | +import { getServerSideConfig } from "@/app/config/server"; |
| 2 | +import { |
| 3 | + CHATGLM_BASE_URL, |
| 4 | + ApiPath, |
| 5 | + ModelProvider, |
| 6 | + ServiceProvider, |
| 7 | +} from "@/app/constant"; |
| 8 | +import { prettyObject } from "@/app/utils/format"; |
| 9 | +import { NextRequest, NextResponse } from "next/server"; |
| 10 | +import { auth } from "@/app/api/auth"; |
| 11 | +import { isModelAvailableInServer } from "@/app/utils/model"; |
| 12 | + |
| 13 | +const serverConfig = getServerSideConfig(); |
| 14 | + |
| 15 | +export async function handle( |
| 16 | + req: NextRequest, |
| 17 | + { params }: { params: { path: string[] } }, |
| 18 | +) { |
| 19 | + console.log("[GLM Route] params ", params); |
| 20 | + |
| 21 | + if (req.method === "OPTIONS") { |
| 22 | + return NextResponse.json({ body: "OK" }, { status: 200 }); |
| 23 | + } |
| 24 | + |
| 25 | + const authResult = auth(req, ModelProvider.ChatGLM); |
| 26 | + if (authResult.error) { |
| 27 | + return NextResponse.json(authResult, { |
| 28 | + status: 401, |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + try { |
| 33 | + const response = await request(req); |
| 34 | + return response; |
| 35 | + } catch (e) { |
| 36 | + console.error("[GLM] ", e); |
| 37 | + return NextResponse.json(prettyObject(e)); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +async function request(req: NextRequest) { |
| 42 | + const controller = new AbortController(); |
| 43 | + |
| 44 | + // alibaba use base url or just remove the path |
| 45 | + let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.ChatGLM, ""); |
| 46 | + |
| 47 | + let baseUrl = serverConfig.chatglmUrl || CHATGLM_BASE_URL; |
| 48 | + |
| 49 | + if (!baseUrl.startsWith("http")) { |
| 50 | + baseUrl = `https://${baseUrl}`; |
| 51 | + } |
| 52 | + |
| 53 | + if (baseUrl.endsWith("/")) { |
| 54 | + baseUrl = baseUrl.slice(0, -1); |
| 55 | + } |
| 56 | + |
| 57 | + console.log("[Proxy] ", path); |
| 58 | + console.log("[Base Url]", baseUrl); |
| 59 | + |
| 60 | + const timeoutId = setTimeout( |
| 61 | + () => { |
| 62 | + controller.abort(); |
| 63 | + }, |
| 64 | + 10 * 60 * 1000, |
| 65 | + ); |
| 66 | + |
| 67 | + const fetchUrl = `${baseUrl}${path}`; |
| 68 | + console.log("[Fetch Url] ", fetchUrl); |
| 69 | + const fetchOptions: RequestInit = { |
| 70 | + headers: { |
| 71 | + "Content-Type": "application/json", |
| 72 | + Authorization: req.headers.get("Authorization") ?? "", |
| 73 | + }, |
| 74 | + method: req.method, |
| 75 | + body: req.body, |
| 76 | + redirect: "manual", |
| 77 | + // @ts-ignore |
| 78 | + duplex: "half", |
| 79 | + signal: controller.signal, |
| 80 | + }; |
| 81 | + |
| 82 | + // #1815 try to refuse some request to some models |
| 83 | + if (serverConfig.customModels && req.body) { |
| 84 | + try { |
| 85 | + const clonedBody = await req.text(); |
| 86 | + fetchOptions.body = clonedBody; |
| 87 | + |
| 88 | + const jsonBody = JSON.parse(clonedBody) as { model?: string }; |
| 89 | + |
| 90 | + // not undefined and is false |
| 91 | + if ( |
| 92 | + isModelAvailableInServer( |
| 93 | + serverConfig.customModels, |
| 94 | + jsonBody?.model as string, |
| 95 | + ServiceProvider.ChatGLM as string, |
| 96 | + ) |
| 97 | + ) { |
| 98 | + return NextResponse.json( |
| 99 | + { |
| 100 | + error: true, |
| 101 | + message: `you are not allowed to use ${jsonBody?.model} model`, |
| 102 | + }, |
| 103 | + { |
| 104 | + status: 403, |
| 105 | + }, |
| 106 | + ); |
| 107 | + } |
| 108 | + } catch (e) { |
| 109 | + console.error(`[GLM] filter`, e); |
| 110 | + } |
| 111 | + } |
| 112 | + try { |
| 113 | + const res = await fetch(fetchUrl, fetchOptions); |
| 114 | + |
| 115 | + // to prevent browser prompt for credentials |
| 116 | + const newHeaders = new Headers(res.headers); |
| 117 | + newHeaders.delete("www-authenticate"); |
| 118 | + // to disable nginx buffering |
| 119 | + newHeaders.set("X-Accel-Buffering", "no"); |
| 120 | + |
| 121 | + return new Response(res.body, { |
| 122 | + status: res.status, |
| 123 | + statusText: res.statusText, |
| 124 | + headers: newHeaders, |
| 125 | + }); |
| 126 | + } finally { |
| 127 | + clearTimeout(timeoutId); |
| 128 | + } |
| 129 | +} |
0 commit comments