From 69f931543fe61680154f2f8b383e3c9f32c2cd23 Mon Sep 17 00:00:00 2001
From: yangyongju <1243354771@qq.com>
Date: Wed, 14 Aug 2024 13:23:45 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=80=E4=BA=9B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
app/api/deepseek.ts | 6 ++--
app/client/api.ts | 4 +--
app/client/platforms/deepseek.ts | 4 +--
app/command.ts | 53 +++++++++++++++++++++++++++-----
app/components/artifacts.tsx | 2 +-
app/components/exporter.tsx | 2 +-
app/components/home.tsx | 35 +++++++++++++++++++++
app/components/sidebar.tsx | 4 +--
app/utils.ts | 5 +++
注意事项.md | 7 +++++
10 files changed, 105 insertions(+), 17 deletions(-)
create mode 100644 注意事项.md
diff --git a/app/api/deepseek.ts b/app/api/deepseek.ts
index 4825ca94e..0eb2e80f8 100644
--- a/app/api/deepseek.ts
+++ b/app/api/deepseek.ts
@@ -33,6 +33,7 @@ export async function handle(
try {
const response = await request(req);
+ console.log("我是从api这边进入的2", response);
return response;
} catch (e) {
console.error("[Deepseek] ", e);
@@ -42,6 +43,7 @@ export async function handle(
async function request(req: NextRequest) {
const controller = new AbortController();
+ console.log("我是从api这边进入的", req.nextUrl.pathname);
// alibaba use base url or just remove the path
let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Deepseek, "");
@@ -57,8 +59,8 @@ async function request(req: NextRequest) {
baseUrl = baseUrl.slice(0, -1);
}
- console.log("[Proxy] ", path);
- console.log("[Base Url]", baseUrl);
+ console.log("[Deepseek Proxy] ", path);
+ console.log("[Deepseek Base Url]", baseUrl);
const timeoutId = setTimeout(
() => {
diff --git a/app/client/api.ts b/app/client/api.ts
index 8ca035b24..38869cdef 100644
--- a/app/client/api.ts
+++ b/app/client/api.ts
@@ -157,7 +157,7 @@ export class ClientApi {
{
from: "human",
value:
- "Share from [NextChat]: https://github.com/Yidadaa/ChatGPT-Next-Web",
+ "Share from [清明上河图小助手]: https://github.com/Yidadaa/ChatGPT-Next-Web",
},
]);
// 敬告二开开发者们,为了开源大模型的发展,请不要修改上述消息,此消息用于后续数据清洗使用
@@ -279,7 +279,7 @@ export function getHeaders() {
const bearerToken = getBearerToken(apiKey, isAzure || isAnthropic);
console.log("bearerToken", bearerToken);
// TODO: remove this 更换 Deepseek 的 api key
- headers["Authorization"] = `Bearer sk-c8e0505e462f49068758ebcc4331c9ee`;
+ headers["Authorization"] = `Bearer sk-94ee98bc9bd14db495d49eb598b69ff0`;
if (bearerToken) {
headers[authHeader] = bearerToken;
} else if (isEnabledAccessControl && validString(accessStore.accessCode)) {
diff --git a/app/client/platforms/deepseek.ts b/app/client/platforms/deepseek.ts
index 0db844a1b..6d09e8cbc 100644
--- a/app/client/platforms/deepseek.ts
+++ b/app/client/platforms/deepseek.ts
@@ -206,14 +206,14 @@ export class DeepseekApi implements LLMApi {
}
},
onmessage(msg) {
- console.log("msg", msg);
+ // console.log("msg", msg);
if (msg.data === "[DONE]" || finished) {
return finish();
}
const text = msg.data;
try {
const json = JSON.parse(text);
- console.log("json", json);
+ // console.log("json", json);
const choices = json.choices as Array<{
delta: { content: string };
diff --git a/app/command.ts b/app/command.ts
index bea4e06f3..3973a1811 100644
--- a/app/command.ts
+++ b/app/command.ts
@@ -1,8 +1,12 @@
+// 导入必要的依赖
import { useEffect } from "react";
import { useSearchParams } from "react-router-dom";
import Locale from "./locales";
+// 定义命令类型和命令接口
+// Command 是一个接受字符串参数并返回 void 的函数类型
type Command = (param: string) => void;
+// Commands 接口定义了可能的命令及其对应的处理函数
interface Commands {
fill?: Command;
submit?: Command;
@@ -11,11 +15,18 @@ interface Commands {
settings?: Command;
}
+/**
+ * 自定义Hook:useCommand
+ * 用于处理URL参数中的命令
+ * @param commands - 命令对象,包含可能的命令及其处理函数
+ */
export function useCommand(commands: Commands = {}) {
+ // 使用 useSearchParams 钩子获取和设置 URL 参数
const [searchParams, setSearchParams] = useSearchParams();
useEffect(() => {
let shouldUpdate = false;
+ // 遍历URL参数,执行对应的命令
searchParams.forEach((param, name) => {
const commandName = name as keyof Commands;
if (typeof commands[commandName] === "function") {
@@ -25,6 +36,7 @@ export function useCommand(commands: Commands = {}) {
}
});
+ // 如果有更新,则设置新的URL参数
if (shouldUpdate) {
setSearchParams(searchParams);
}
@@ -32,19 +44,30 @@ export function useCommand(commands: Commands = {}) {
}, [searchParams, commands]);
}
+// 定义聊天命令接口
interface ChatCommands {
- new?: Command;
- newm?: Command;
- next?: Command;
- prev?: Command;
- clear?: Command;
- del?: Command;
+ new?: Command; // 新建聊天
+ newm?: Command; // 新建面具聊天
+ next?: Command; // 下一个聊天
+ prev?: Command; // 上一个聊天
+ clear?: Command; // 清除聊天历史
+ del?: Command; // 删除当前聊天
}
-// Compatible with Chinese colon character ":"
+// 定义聊天命令前缀(兼容中文冒号和英文冒号)
export const ChatCommandPrefix = /^[::]/;
+/**
+ * 自定义Hook:useChatCommand
+ * 用于处理聊天命令
+ * @param commands - 聊天命令对象,包含可能的命令及其处理函数
+ */
export function useChatCommand(commands: ChatCommands = {}) {
+ /**
+ * 提取命令
+ * @param userInput - 用户输入的字符串
+ * @returns 提取出的命令(去掉前缀)
+ */
function extract(userInput: string) {
const match = userInput.match(ChatCommandPrefix);
if (match) {
@@ -53,6 +76,11 @@ export function useChatCommand(commands: ChatCommands = {}) {
return userInput as keyof ChatCommands;
}
+ /**
+ * 搜索匹配的命令
+ * @param userInput - 用户输入的字符串
+ * @returns 匹配的命令列表,包含标题和内容
+ */
function search(userInput: string) {
const input = extract(userInput);
const desc = Locale.Chat.Commands;
@@ -64,6 +92,11 @@ export function useChatCommand(commands: ChatCommands = {}) {
}));
}
+ /**
+ * 匹配并执行命令
+ * @param userInput - 用户输入的字符串
+ * @returns 包含是否匹配和执行函数的对象
+ */
function match(userInput: string) {
const command = extract(userInput);
const matched = typeof commands[command] === "function";
@@ -76,3 +109,9 @@ export function useChatCommand(commands: ChatCommands = {}) {
return { match, search };
}
+
+// 参考资料:
+// 1. React Router useSearchParams: https://reactrouter.com/web/api/Hooks/usesearchparams
+// 2. React useEffect Hook: https://reactjs.org/docs/hooks-effect.html
+// 3. TypeScript Interfaces: https://www.typescriptlang.org/docs/handbook/interfaces.html
+// 4. JavaScript Regular Expressions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
diff --git a/app/components/artifacts.tsx b/app/components/artifacts.tsx
index 326891e73..a46fbf0d1 100644
--- a/app/components/artifacts.tsx
+++ b/app/components/artifacts.tsx
@@ -208,7 +208,7 @@ export function Artifacts() {