mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-08-09 05:34:01 +08:00
Merge branch 'Yidadaa:main' into update
This commit is contained in:
@@ -3,8 +3,6 @@ import { getServerSideConfig } from "../config/server";
|
||||
import md5 from "spark-md5";
|
||||
import { ACCESS_CODE_PREFIX } from "../constant";
|
||||
|
||||
const serverConfig = getServerSideConfig();
|
||||
|
||||
function getIP(req: NextRequest) {
|
||||
let ip = req.ip ?? req.headers.get("x-real-ip");
|
||||
const forwardedFor = req.headers.get("x-forwarded-for");
|
||||
@@ -34,6 +32,7 @@ export function auth(req: NextRequest) {
|
||||
|
||||
const hashedCode = md5.hash(accessCode ?? "").trim();
|
||||
|
||||
const serverConfig = getServerSideConfig();
|
||||
console.log("[Auth] allowed hashed codes: ", [...serverConfig.codes]);
|
||||
console.log("[Auth] got access code:", accessCode);
|
||||
console.log("[Auth] hashed access code:", hashedCode);
|
||||
|
@@ -25,10 +25,6 @@ export async function requestOpenai(req: NextRequest) {
|
||||
console.log("[Org ID]", process.env.OPENAI_ORG_ID);
|
||||
}
|
||||
|
||||
if (!authValue || !authValue.startsWith("Bearer sk-")) {
|
||||
console.error("[OpenAI Request] invalid api key provided", authValue);
|
||||
}
|
||||
|
||||
return fetch(`${baseUrl}/${openaiPath}`, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
|
@@ -71,9 +71,13 @@ export class ChatGPTApi implements LLMApi {
|
||||
|
||||
if (shouldStream) {
|
||||
let responseText = "";
|
||||
let finished = false;
|
||||
|
||||
const finish = () => {
|
||||
options.onFinish(responseText);
|
||||
if (!finished) {
|
||||
options.onFinish(responseText);
|
||||
finished = true;
|
||||
}
|
||||
};
|
||||
|
||||
controller.signal.onabort = finish;
|
||||
@@ -82,30 +86,44 @@ export class ChatGPTApi implements LLMApi {
|
||||
...chatPayload,
|
||||
async onopen(res) {
|
||||
clearTimeout(requestTimeoutId);
|
||||
if (
|
||||
res.ok &&
|
||||
res.headers.get("content-type") !== EventStreamContentType
|
||||
) {
|
||||
responseText += await res.clone().json();
|
||||
const contentType = res.headers.get("content-type");
|
||||
console.log(
|
||||
"[OpenAI] request response content type: ",
|
||||
contentType,
|
||||
);
|
||||
|
||||
if (contentType?.startsWith("text/plain")) {
|
||||
responseText = await res.clone().text();
|
||||
return finish();
|
||||
}
|
||||
if (res.status === 401) {
|
||||
let extraInfo = { error: undefined };
|
||||
|
||||
if (
|
||||
!res.ok ||
|
||||
res.headers.get("content-type") !== EventStreamContentType ||
|
||||
res.status !== 200
|
||||
) {
|
||||
const responseTexts = [responseText];
|
||||
let extraInfo = await res.clone().text();
|
||||
try {
|
||||
extraInfo = await res.clone().json();
|
||||
const resJson = await res.clone().json();
|
||||
extraInfo = prettyObject(resJson);
|
||||
} catch {}
|
||||
|
||||
responseText += "\n\n" + Locale.Error.Unauthorized;
|
||||
|
||||
if (extraInfo.error) {
|
||||
responseText += "\n\n" + prettyObject(extraInfo);
|
||||
if (res.status === 401) {
|
||||
responseTexts.push(Locale.Error.Unauthorized);
|
||||
}
|
||||
|
||||
if (extraInfo) {
|
||||
responseTexts.push(extraInfo);
|
||||
}
|
||||
|
||||
responseText = responseTexts.join("\n\n");
|
||||
|
||||
return finish();
|
||||
}
|
||||
},
|
||||
onmessage(msg) {
|
||||
if (msg.data === "[DONE]") {
|
||||
if (msg.data === "[DONE]" || finished) {
|
||||
return finish();
|
||||
}
|
||||
const text = msg.data;
|
||||
|
@@ -53,7 +53,7 @@ import chatStyle from "./chat.module.scss";
|
||||
|
||||
import { ListItem, Modal, showModal } from "./ui-lib";
|
||||
import { useLocation, useNavigate } from "react-router-dom";
|
||||
import { LAST_INPUT_KEY, Path } from "../constant";
|
||||
import { LAST_INPUT_KEY, Path, REQUEST_TIMEOUT_MS } from "../constant";
|
||||
import { Avatar } from "./emoji";
|
||||
import { MaskAvatar, MaskConfig } from "./mask";
|
||||
import { useMaskStore } from "../store/mask";
|
||||
@@ -487,6 +487,16 @@ export function Chat() {
|
||||
|
||||
// stop response
|
||||
const onUserStop = (messageId: number) => {
|
||||
chatStore.updateCurrentSession((session) => {
|
||||
const stopTiming = Date.now() - REQUEST_TIMEOUT_MS;
|
||||
session.messages.forEach((m) => {
|
||||
// check if should stop all stale messages
|
||||
if (m.streaming && new Date(m.date).getTime() < stopTiming) {
|
||||
m.isError = false;
|
||||
m.streaming = false;
|
||||
}
|
||||
});
|
||||
});
|
||||
ChatControllerPool.stop(sessionIndex, messageId);
|
||||
};
|
||||
|
||||
|
@@ -7,7 +7,7 @@ import Locale from "../locales";
|
||||
import { showToast } from "../components/ui-lib";
|
||||
import { ModelType } from "./config";
|
||||
import { createEmptyMask, Mask } from "./mask";
|
||||
import { StoreKey } from "../constant";
|
||||
import { REQUEST_TIMEOUT_MS, StoreKey } from "../constant";
|
||||
import { api, RequestMessage } from "../client/api";
|
||||
import { ChatControllerPool } from "../client/controller";
|
||||
import { prettyObject } from "../utils/format";
|
||||
|
Reference in New Issue
Block a user