Merge pull request #1653 from Yidadaa/bugfix-0520

feat: close #1626 hide context prompts in mask config
This commit is contained in:
Yifei Zhang
2023-05-20 20:20:35 +08:00
committed by GitHub
12 changed files with 131 additions and 98 deletions

View File

@@ -55,10 +55,6 @@ export function auth(req: NextRequest) {
req.headers.set("Authorization", `Bearer ${apiKey}`);
} else {
console.log("[Auth] admin did not provide an api key");
return {
error: serverConfig.baseUrl?.includes(OPENAI_URL),
msg: "admin did not provide an api key",
};
}
} else {
console.log("[Auth] use user api key");

View File

@@ -28,6 +28,7 @@ export const ChatControllerPool = {
remove(sessionIndex: number, messageId: number) {
const key = this.key(sessionIndex, messageId);
this.controllers[key]?.abort();
delete this.controllers[key];
},

View File

@@ -6,7 +6,7 @@ import Locale from "../../locales";
import {
EventStreamContentType,
fetchEventSource,
} from "@microsoft/fetch-event-source";
} from "@fortaine/fetch-event-source";
import { prettyObject } from "@/app/utils/format";
export class ChatGPTApi implements LLMApi {
@@ -145,6 +145,7 @@ export class ChatGPTApi implements LLMApi {
},
onerror(e) {
options.onError?.(e);
throw e;
},
openWhenHidden: true,
});

View File

@@ -58,6 +58,7 @@ import { Avatar } from "./emoji";
import { MaskAvatar, MaskConfig } from "./mask";
import { useMaskStore } from "../store/mask";
import { useCommand } from "../command";
import { prettyObject } from "../utils/format";
const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
loading: () => <LoadingIcon />,
@@ -496,13 +497,17 @@ export function Chat() {
const stopTiming = Date.now() - REQUEST_TIMEOUT_MS;
session.messages.forEach((m) => {
// check if should stop all stale messages
if (new Date(m.date).getTime() < stopTiming) {
if (m.isError || new Date(m.date).getTime() < stopTiming) {
if (m.streaming) {
m.streaming = false;
}
if (m.content.length === 0) {
m.content = "No content in this message.";
m.isError = true;
m.content = prettyObject({
error: true,
message: "empty response",
});
}
}
});
@@ -580,7 +585,9 @@ export function Chat() {
inputRef.current?.focus();
};
const context: RenderMessage[] = session.mask.context.slice();
const context: RenderMessage[] = session.mask.hideContext
? []
: session.mask.context.slice();
const accessStore = useAccessStore();

View File

@@ -104,25 +104,41 @@ export function MaskConfig(props: {
></input>
</ListItem>
<ListItem
title={Locale.Mask.Config.Sync.Title}
subTitle={Locale.Mask.Config.Sync.SubTitle}
title={Locale.Mask.Config.HideContext.Title}
subTitle={Locale.Mask.Config.HideContext.SubTitle}
>
<input
type="checkbox"
checked={props.mask.syncGlobalConfig}
checked={props.mask.hideContext}
onChange={(e) => {
if (
e.currentTarget.checked &&
confirm(Locale.Mask.Config.Sync.Confirm)
) {
props.updateMask((mask) => {
mask.syncGlobalConfig = e.currentTarget.checked;
mask.modelConfig = { ...globalConfig.modelConfig };
});
}
props.updateMask((mask) => {
mask.hideContext = e.currentTarget.checked;
});
}}
></input>
</ListItem>
{props.shouldSyncFromGlobal ? (
<ListItem
title={Locale.Mask.Config.Sync.Title}
subTitle={Locale.Mask.Config.Sync.SubTitle}
>
<input
type="checkbox"
checked={props.mask.syncGlobalConfig}
onChange={(e) => {
if (
e.currentTarget.checked &&
confirm(Locale.Mask.Config.Sync.Confirm)
) {
props.updateMask((mask) => {
mask.syncGlobalConfig = e.currentTarget.checked;
mask.modelConfig = { ...globalConfig.modelConfig };
});
}
}}
></input>
</ListItem>
) : null}
</List>
<List>

View File

@@ -210,6 +210,10 @@ const cn = {
SubTitle: "当前对话是否使用全局模型设置",
Confirm: "当前对话的自定义设置将会被自动覆盖,确认启用全局设置?",
},
HideContext: {
Title: "隐藏预设对话",
SubTitle: "隐藏后预设对话不会出现在聊天界面",
},
},
},
NewChat: {

View File

@@ -213,6 +213,10 @@ const en: RequiredLocaleType = {
SubTitle: "Use global config in this chat",
Confirm: "Confirm to override custom config with global config?",
},
HideContext: {
Title: "Hide Context Prompts",
SubTitle: "Do not show in-context prompts in chat",
},
},
},
NewChat: {
@@ -221,7 +225,7 @@ const en: RequiredLocaleType = {
Title: "Pick a Mask",
SubTitle: "Chat with the Soul behind the Mask",
More: "Find More",
NotShow: "Not Show Again",
NotShow: "Dont Show Again",
ConfirmNoShow: "Confirm to disableYou can enable it in settings later.",
},

View File

@@ -5,7 +5,7 @@ import { trimTopic } from "../utils";
import Locale from "../locales";
import { showToast } from "../components/ui-lib";
import { ModelType, useAppConfig } from "./config";
import { ModelType } from "./config";
import { createEmptyMask, Mask } from "./mask";
import { StoreKey } from "../constant";
import { api, RequestMessage } from "../client/api";
@@ -277,13 +277,17 @@ export const useChatStore = create<ChatStore>()(
config: { ...modelConfig, stream: true },
onUpdate(message) {
botMessage.streaming = true;
botMessage.content = message;
if (message) {
botMessage.content = message;
}
set(() => ({}));
},
onFinish(message) {
botMessage.streaming = false;
botMessage.content = message;
get().onNewMessage(botMessage);
if (message) {
botMessage.content = message;
get().onNewMessage(botMessage);
}
ChatControllerPool.remove(
sessionIndex,
botMessage.id ?? messageIndex,
@@ -292,12 +296,12 @@ export const useChatStore = create<ChatStore>()(
},
onError(error) {
const isAborted = error.message.includes("aborted");
if (
botMessage.content !== Locale.Error.Unauthorized &&
!isAborted
) {
botMessage.content += "\n\n" + prettyObject(error);
}
botMessage.content =
"\n\n" +
prettyObject({
error: true,
message: error.message,
});
botMessage.streaming = false;
userMessage.isError = !isAborted;
botMessage.isError = !isAborted;
@@ -308,7 +312,7 @@ export const useChatStore = create<ChatStore>()(
botMessage.id ?? messageIndex,
);
console.error("[Chat] error ", error);
console.error("[Chat] failed ", error);
},
onController(controller) {
// collect controller for stop/retry

View File

@@ -10,6 +10,7 @@ export type Mask = {
id: number;
avatar: string;
name: string;
hideContext?: boolean;
context: ChatMessage[];
syncGlobalConfig?: boolean;
modelConfig: ModelConfig;

View File

@@ -1,8 +1,7 @@
export function prettyObject(msg: any) {
const prettyMsg = [
"```json\n",
JSON.stringify(msg, null, " "),
"\n```",
].join("");
if (typeof msg !== "string") {
msg = JSON.stringify(msg, null, " ");
}
const prettyMsg = ["```json", msg, "```"].join("\n");
return prettyMsg;
}