Skip to content

Commit f27b25a

Browse files
authored
Merge pull request ChatGPTNextWeb#1653 from Yidadaa/bugfix-0520
feat: close ChatGPTNextWeb#1626 hide context prompts in mask config
2 parents 697c7a8 + 6d8c7ba commit f27b25a

File tree

12 files changed

+139
-106
lines changed

12 files changed

+139
-106
lines changed

app/api/auth.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ export function auth(req: NextRequest) {
5555
req.headers.set("Authorization", `Bearer ${apiKey}`);
5656
} else {
5757
console.log("[Auth] admin did not provide an api key");
58-
return {
59-
error: serverConfig.baseUrl?.includes(OPENAI_URL),
60-
msg: "admin did not provide an api key",
61-
};
6258
}
6359
} else {
6460
console.log("[Auth] use user api key");

app/client/controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const ChatControllerPool = {
2828

2929
remove(sessionIndex: number, messageId: number) {
3030
const key = this.key(sessionIndex, messageId);
31+
this.controllers[key]?.abort();
3132
delete this.controllers[key];
3233
},
3334

app/client/platforms/openai.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Locale from "../../locales";
66
import {
77
EventStreamContentType,
88
fetchEventSource,
9-
} from "@microsoft/fetch-event-source";
9+
} from "@fortaine/fetch-event-source";
1010
import { prettyObject } from "@/app/utils/format";
1111

1212
export class ChatGPTApi implements LLMApi {
@@ -145,6 +145,7 @@ export class ChatGPTApi implements LLMApi {
145145
},
146146
onerror(e) {
147147
options.onError?.(e);
148+
throw e;
148149
},
149150
openWhenHidden: true,
150151
});

app/components/chat.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import { Avatar } from "./emoji";
5858
import { MaskAvatar, MaskConfig } from "./mask";
5959
import { useMaskStore } from "../store/mask";
6060
import { useCommand } from "../command";
61+
import { prettyObject } from "../utils/format";
6162

6263
const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
6364
loading: () => <LoadingIcon />,
@@ -496,13 +497,17 @@ export function Chat() {
496497
const stopTiming = Date.now() - REQUEST_TIMEOUT_MS;
497498
session.messages.forEach((m) => {
498499
// check if should stop all stale messages
499-
if (new Date(m.date).getTime() < stopTiming) {
500+
if (m.isError || new Date(m.date).getTime() < stopTiming) {
500501
if (m.streaming) {
501502
m.streaming = false;
502503
}
503504

504505
if (m.content.length === 0) {
505-
m.content = "No content in this message.";
506+
m.isError = true;
507+
m.content = prettyObject({
508+
error: true,
509+
message: "empty response",
510+
});
506511
}
507512
}
508513
});
@@ -580,7 +585,9 @@ export function Chat() {
580585
inputRef.current?.focus();
581586
};
582587

583-
const context: RenderMessage[] = session.mask.context.slice();
588+
const context: RenderMessage[] = session.mask.hideContext
589+
? []
590+
: session.mask.context.slice();
584591

585592
const accessStore = useAccessStore();
586593

app/components/mask.tsx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,25 +104,41 @@ export function MaskConfig(props: {
104104
></input>
105105
</ListItem>
106106
<ListItem
107-
title={Locale.Mask.Config.Sync.Title}
108-
subTitle={Locale.Mask.Config.Sync.SubTitle}
107+
title={Locale.Mask.Config.HideContext.Title}
108+
subTitle={Locale.Mask.Config.HideContext.SubTitle}
109109
>
110110
<input
111111
type="checkbox"
112-
checked={props.mask.syncGlobalConfig}
112+
checked={props.mask.hideContext}
113113
onChange={(e) => {
114-
if (
115-
e.currentTarget.checked &&
116-
confirm(Locale.Mask.Config.Sync.Confirm)
117-
) {
118-
props.updateMask((mask) => {
119-
mask.syncGlobalConfig = e.currentTarget.checked;
120-
mask.modelConfig = { ...globalConfig.modelConfig };
121-
});
122-
}
114+
props.updateMask((mask) => {
115+
mask.hideContext = e.currentTarget.checked;
116+
});
123117
}}
124118
></input>
125119
</ListItem>
120+
{props.shouldSyncFromGlobal ? (
121+
<ListItem
122+
title={Locale.Mask.Config.Sync.Title}
123+
subTitle={Locale.Mask.Config.Sync.SubTitle}
124+
>
125+
<input
126+
type="checkbox"
127+
checked={props.mask.syncGlobalConfig}
128+
onChange={(e) => {
129+
if (
130+
e.currentTarget.checked &&
131+
confirm(Locale.Mask.Config.Sync.Confirm)
132+
) {
133+
props.updateMask((mask) => {
134+
mask.syncGlobalConfig = e.currentTarget.checked;
135+
mask.modelConfig = { ...globalConfig.modelConfig };
136+
});
137+
}
138+
}}
139+
></input>
140+
</ListItem>
141+
) : null}
126142
</List>
127143

128144
<List>

app/locales/cn.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ const cn = {
210210
SubTitle: "当前对话是否使用全局模型设置",
211211
Confirm: "当前对话的自定义设置将会被自动覆盖,确认启用全局设置?",
212212
},
213+
HideContext: {
214+
Title: "隐藏预设对话",
215+
SubTitle: "隐藏后预设对话不会出现在聊天界面",
216+
},
213217
},
214218
},
215219
NewChat: {

app/locales/en.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ const en: RequiredLocaleType = {
213213
SubTitle: "Use global config in this chat",
214214
Confirm: "Confirm to override custom config with global config?",
215215
},
216+
HideContext: {
217+
Title: "Hide Context Prompts",
218+
SubTitle: "Do not show in-context prompts in chat",
219+
},
216220
},
217221
},
218222
NewChat: {
@@ -221,7 +225,7 @@ const en: RequiredLocaleType = {
221225
Title: "Pick a Mask",
222226
SubTitle: "Chat with the Soul behind the Mask",
223227
More: "Find More",
224-
NotShow: "Not Show Again",
228+
NotShow: "Dont Show Again",
225229
ConfirmNoShow: "Confirm to disable?You can enable it in settings later.",
226230
},
227231

app/store/chat.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { trimTopic } from "../utils";
55

66
import Locale from "../locales";
77
import { showToast } from "../components/ui-lib";
8-
import { ModelType, useAppConfig } from "./config";
8+
import { ModelType } from "./config";
99
import { createEmptyMask, Mask } from "./mask";
1010
import { StoreKey } from "../constant";
1111
import { api, RequestMessage } from "../client/api";
@@ -277,13 +277,17 @@ export const useChatStore = create<ChatStore>()(
277277
config: { ...modelConfig, stream: true },
278278
onUpdate(message) {
279279
botMessage.streaming = true;
280-
botMessage.content = message;
280+
if (message) {
281+
botMessage.content = message;
282+
}
281283
set(() => ({}));
282284
},
283285
onFinish(message) {
284286
botMessage.streaming = false;
285-
botMessage.content = message;
286-
get().onNewMessage(botMessage);
287+
if (message) {
288+
botMessage.content = message;
289+
get().onNewMessage(botMessage);
290+
}
287291
ChatControllerPool.remove(
288292
sessionIndex,
289293
botMessage.id ?? messageIndex,
@@ -292,12 +296,12 @@ export const useChatStore = create<ChatStore>()(
292296
},
293297
onError(error) {
294298
const isAborted = error.message.includes("aborted");
295-
if (
296-
botMessage.content !== Locale.Error.Unauthorized &&
297-
!isAborted
298-
) {
299-
botMessage.content += "\n\n" + prettyObject(error);
300-
}
299+
botMessage.content =
300+
"\n\n" +
301+
prettyObject({
302+
error: true,
303+
message: error.message,
304+
});
301305
botMessage.streaming = false;
302306
userMessage.isError = !isAborted;
303307
botMessage.isError = !isAborted;
@@ -308,7 +312,7 @@ export const useChatStore = create<ChatStore>()(
308312
botMessage.id ?? messageIndex,
309313
);
310314

311-
console.error("[Chat] error ", error);
315+
console.error("[Chat] failed ", error);
312316
},
313317
onController(controller) {
314318
// collect controller for stop/retry

app/store/mask.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type Mask = {
1010
id: number;
1111
avatar: string;
1212
name: string;
13+
hideContext?: boolean;
1314
context: ChatMessage[];
1415
syncGlobalConfig?: boolean;
1516
modelConfig: ModelConfig;

app/utils/format.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
export function prettyObject(msg: any) {
2-
const prettyMsg = [
3-
"```json\n",
4-
JSON.stringify(msg, null, " "),
5-
"\n```",
6-
].join("");
2+
if (typeof msg !== "string") {
3+
msg = JSON.stringify(msg, null, " ");
4+
}
5+
const prettyMsg = ["```json", msg, "```"].join("\n");
76
return prettyMsg;
87
}

0 commit comments

Comments
 (0)