65 lines
2.0 KiB
TypeScript
65 lines
2.0 KiB
TypeScript
import { Context } from "../../types"
|
|
|
|
/**
|
|
* 处理模型代理请求
|
|
* @param req
|
|
* @returns
|
|
*/
|
|
export const manageModelProxyReq = async (ctx: Context) => {
|
|
const { req, headers, logger, path } = ctx
|
|
if (!path.exact("/v1/models")) {
|
|
const auth = headers.get("Authorization")
|
|
logger.info("开始模型代理转发", { Authorization: auth })
|
|
if (!auth || auth !== "Bearer sk-21a2ce1c2ee94bc2933798eac1bbcadc") {
|
|
return ctx.genResp.forbidden("Authorization required")
|
|
}
|
|
}
|
|
const PROXY_URL =
|
|
"http://ms-13871-nstruct-lmdeploy-2-0109140455.kscn-tj5-prod2-cloudml.xiaomi.srv"
|
|
|
|
// 发起代理请求
|
|
const response = await fetch(PROXY_URL + new URL(req.url).pathname, {
|
|
method: req.method,
|
|
body: req.body,
|
|
headers: {
|
|
// 转发必要的请求头
|
|
"Content-Type": headers.get("Content-Type") || "application/json",
|
|
Accept: headers.get("Accept") || "*/*",
|
|
},
|
|
})
|
|
|
|
// 获取原始响应的所有头信息
|
|
const responseHeaders = new Headers(response.headers)
|
|
|
|
// 添加CORS响应头
|
|
responseHeaders.set("Access-Control-Allow-Origin", "*")
|
|
responseHeaders.set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
|
responseHeaders.set(
|
|
"Access-Control-Allow-Headers",
|
|
"Content-Type, Authorization"
|
|
)
|
|
responseHeaders.set("Access-Control-Expose-Headers", "Content-Type")
|
|
|
|
// 检查是否为流式响应
|
|
if (response.headers.get("Content-Type")?.includes("text/event-stream")) {
|
|
// 处理流式响应
|
|
const { readable, writable } = new TransformStream()
|
|
response.body
|
|
?.pipeTo(writable)
|
|
.catch((err) => logger.error("流式响应传输失败", { error: err.message }))
|
|
|
|
return new Response(readable, {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
headers: responseHeaders,
|
|
})
|
|
} else {
|
|
// 处理非流式响应
|
|
return new Response(response.body, {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
headers: responseHeaders,
|
|
})
|
|
}
|
|
}
|