mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-09-08 01:06:58 +08:00
feat: mix handlers of proxy server in providers
This commit is contained in:
@@ -3,3 +3,5 @@ export * from "./types";
|
||||
export * from "./locale";
|
||||
|
||||
export * from "./utils";
|
||||
|
||||
export const modelNameRequestHeader = "x-nextchat-model-name";
|
||||
|
@@ -1,4 +1,6 @@
|
||||
import { RequestMessage } from "../api";
|
||||
import { getServerSideConfig } from "@/app/config/server";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export { type RequestMessage };
|
||||
|
||||
@@ -152,6 +154,9 @@ export type SettingItem<SettingKeys extends string = ""> =
|
||||
// ===================================== Provider Settings Types end ======================================
|
||||
|
||||
// ===================================== Provider Template Types start ======================================
|
||||
|
||||
export type ServerConfig = ReturnType<typeof getServerSideConfig>;
|
||||
|
||||
export interface IProviderTemplate<
|
||||
SettingKeys extends string,
|
||||
NAME extends string,
|
||||
@@ -159,6 +164,12 @@ export interface IProviderTemplate<
|
||||
> {
|
||||
readonly name: NAME;
|
||||
|
||||
readonly apiRouteRootName: `/api/provider/${NAME}`;
|
||||
|
||||
readonly allowedApiMethods: Array<
|
||||
"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS"
|
||||
>;
|
||||
|
||||
readonly metas: Meta;
|
||||
|
||||
readonly providerMeta: {
|
||||
@@ -170,17 +181,31 @@ export interface IProviderTemplate<
|
||||
streamChat(
|
||||
payload: InternalChatRequestPayload<SettingKeys>,
|
||||
handlers: ChatHandlers,
|
||||
fetch: typeof window.fetch,
|
||||
): AbortController;
|
||||
|
||||
chat(
|
||||
payload: InternalChatRequestPayload<SettingKeys>,
|
||||
fetch: typeof window.fetch,
|
||||
): Promise<StandChatReponseMessage>;
|
||||
|
||||
getAvailableModels?(
|
||||
providerConfig: InternalChatRequestPayload<SettingKeys>["providerConfig"],
|
||||
): Promise<ModelInfo[]>;
|
||||
|
||||
readonly runtime: "edge";
|
||||
readonly preferredRegion: "auto" | "global" | "home" | string | string[];
|
||||
|
||||
serverSideRequestHandler(
|
||||
req: NextRequest & {
|
||||
subpath: string;
|
||||
},
|
||||
serverConfig: ServerConfig,
|
||||
): Promise<NextResponse>;
|
||||
}
|
||||
|
||||
export type ProviderTemplate = IProviderTemplate<any, any, any>;
|
||||
|
||||
export interface Serializable<Snapshot> {
|
||||
serialize(): Snapshot;
|
||||
}
|
||||
|
@@ -1,4 +1,6 @@
|
||||
import { RequestMessage } from "./types";
|
||||
import { NextRequest } from "next/server";
|
||||
import { RequestMessage, ServerConfig } from "./types";
|
||||
import { cloneDeep } from "lodash-es";
|
||||
|
||||
export function getMessageTextContent(message: RequestMessage) {
|
||||
if (typeof message.content === "string") {
|
||||
@@ -24,3 +26,63 @@ export function getMessageImages(message: RequestMessage): string[] {
|
||||
}
|
||||
return urls;
|
||||
}
|
||||
|
||||
export function getIP(req: NextRequest) {
|
||||
let ip = req.ip ?? req.headers.get("x-real-ip");
|
||||
const forwardedFor = req.headers.get("x-forwarded-for");
|
||||
|
||||
if (!ip && forwardedFor) {
|
||||
ip = forwardedFor.split(",").at(0) ?? "";
|
||||
}
|
||||
|
||||
return ip;
|
||||
}
|
||||
|
||||
export function formatUrl(baseUrl?: string) {
|
||||
if (baseUrl && !baseUrl.startsWith("http")) {
|
||||
baseUrl = `https://${baseUrl}`;
|
||||
}
|
||||
if (baseUrl?.endsWith("/")) {
|
||||
baseUrl = baseUrl.slice(0, -1);
|
||||
}
|
||||
|
||||
return baseUrl;
|
||||
}
|
||||
|
||||
function travel(
|
||||
config: ServerConfig,
|
||||
keys: Array<keyof ServerConfig>,
|
||||
handle: (prop: any) => any,
|
||||
): ServerConfig {
|
||||
const copiedConfig = cloneDeep(config);
|
||||
keys.forEach((k) => {
|
||||
copiedConfig[k] = handle(copiedConfig[k] as string) as never;
|
||||
});
|
||||
return copiedConfig;
|
||||
}
|
||||
|
||||
export const makeUrlsUsable = (
|
||||
config: ServerConfig,
|
||||
keys: Array<keyof ServerConfig>,
|
||||
) => travel(config, keys, formatUrl);
|
||||
|
||||
export const disableSystemApiKey = (
|
||||
config: ServerConfig,
|
||||
keys: Array<keyof ServerConfig>,
|
||||
forbidden: boolean,
|
||||
) =>
|
||||
travel(config, keys, (p) => {
|
||||
return forbidden ? undefined : p;
|
||||
});
|
||||
|
||||
export function isSameOrigin(requestUrl: string) {
|
||||
var a = document.createElement("a");
|
||||
a.href = requestUrl;
|
||||
|
||||
// 检查协议、主机名和端口号是否与当前页面相同
|
||||
return (
|
||||
a.protocol === window.location.protocol &&
|
||||
a.hostname === window.location.hostname &&
|
||||
a.port === window.location.port
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user