diff --git a/app/api/openai/route.ts b/app/api/openai/route.ts index 5bc317e5..5ddb0f4c 100644 --- a/app/api/openai/route.ts +++ b/app/api/openai/route.ts @@ -3,8 +3,10 @@ import { requestOpenai } from "../common"; async function makeRequest(req: NextRequest) { try { - const res = await requestOpenai(req); - return new Response(res.body); + const api = await requestOpenai(req); + const res = new NextResponse(api.body); + res.headers.set('Content-Type', 'application/json'); + return res; } catch (e) { console.error("[OpenAI] ", req.body, e); return NextResponse.json( diff --git a/app/requests.ts b/app/requests.ts index f4db7a1b..56fd6cb5 100644 --- a/app/requests.ts +++ b/app/requests.ts @@ -2,6 +2,10 @@ import type { ChatRequest, ChatReponse } from "./api/openai/typing"; import { filterConfig, Message, ModelConfig, useAccessStore } from "./store"; import Locale from "./locales"; +if (!Array.prototype.at) { + require('array.prototype.at/auto'); +} + const TIME_OUT_MS = 30000; const makeRequestParam = ( diff --git a/app/store/app.ts b/app/store/app.ts index 6ab3229a..cc52a3c7 100644 --- a/app/store/app.ts +++ b/app/store/app.ts @@ -11,6 +11,10 @@ import { trimTopic } from "../utils"; import Locale from "../locales"; +if (!Array.prototype.at) { + require('array.prototype.at/auto'); +} + export type Message = ChatCompletionResponseMessage & { date: string; streaming?: boolean; diff --git a/app/utils.ts b/app/utils.ts index 64120df4..1fb3d316 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -2,15 +2,7 @@ import { showToast } from "./components/ui-lib"; import Locale from "./locales"; export function trimTopic(topic: string) { - const s = topic.split(""); - let lastChar = s.at(-1); // 获取 s 的最后一个字符 - let pattern = /[,。!?、,.!?]/; // 定义匹配中文和英文标点符号的正则表达式 - while (lastChar && pattern.test(lastChar!)) { - s.pop(); - lastChar = s.at(-1); - } - - return s.join(""); + return topic.replace(/[,。!?、,.!?]*$/, ""); } export function copyToClipboard(text: string) { diff --git a/package.json b/package.json index eb17000e..7c6832ed 100644 --- a/package.json +++ b/package.json @@ -23,9 +23,9 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-markdown": "^8.0.5", - "remark-breaks": "^3.0.2", "rehype-katex": "^6.0.2", "rehype-prism-plus": "^1.5.1", + "remark-breaks": "^3.0.2", "remark-gfm": "^3.0.1", "remark-math": "^5.1.1", "sass": "^1.59.2", @@ -39,6 +39,7 @@ "@types/react-dom": "^18.0.11", "@types/react-katex": "^3.0.0", "@types/spark-md5": "^3.0.2", + "array.prototype.at": "^1.1.1", "cross-env": "^7.0.3", "eslint": "^8.36.0", "eslint-config-next": "13.2.3", diff --git a/yarn.lock b/yarn.lock index 9f98a244..246b818b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1570,6 +1570,16 @@ array-union@^2.1.0: resolved "https://registry.yarnpkg.com/array-union/-/array-union-2.1.0.tgz#b798420adbeb1de828d84acd8a2e23d3efe85e8d" integrity sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw== +array.prototype.at@^1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/array.prototype.at/-/array.prototype.at-1.1.1.tgz#6deda3cd3c704afa16361387ea344e0b8d8831b5" + integrity sha512-n/wYNLJy/fVEU9EGPt2ww920hy1XX3XB2yTREFy1QsxctBgQV/tZIwg1G8jVxELna4pLCzg/xvvS/DDXtI4NNg== + dependencies: + call-bind "^1.0.2" + define-properties "^1.1.4" + es-abstract "^1.20.4" + es-shim-unscopables "^1.0.0" + array.prototype.flat@^1.3.1: version "1.3.1" resolved "https://registry.yarnpkg.com/array.prototype.flat/-/array.prototype.flat-1.3.1.tgz#ffc6576a7ca3efc2f46a143b9d1dda9b4b3cf5e2"