feat: #112 add edit chat title

This commit is contained in:
Yifei Zhang 2023-03-29 16:02:50 +00:00
parent 08f3c7026d
commit 45088a3e06
7 changed files with 40 additions and 14 deletions

View File

@ -1,6 +1,6 @@
{ {
"./app/**/*.{js,ts,jsx,tsx,json,html,css,scss,md}": [ "./app/**/*.{js,ts,jsx,tsx,json,html,css,md}": [
"eslint --fix", "eslint --fix",
"prettier --write" "prettier --write"
] ]
} }

View File

@ -221,6 +221,14 @@
margin-bottom: 100px; margin-bottom: 100px;
} }
.chat-body-title {
cursor: pointer;
&:hover {
text-decoration: underline;
}
}
.chat-message { .chat-message {
display: flex; display: flex;
flex-direction: row; flex-direction: row;

View File

@ -333,7 +333,17 @@ export function Chat(props: {
className={styles["window-header-title"]} className={styles["window-header-title"]}
onClick={props?.showSideBar} onClick={props?.showSideBar}
> >
<div className={styles["window-header-main-title"]}> <div
className={`${styles["window-header-main-title"]} ${styles["chat-body-title"]}`}
onClick={() => {
const newTopic = prompt(Locale.Chat.Rename, session.topic);
if (newTopic && newTopic !== session.topic) {
chatStore.updateCurrentSession(
(session) => (session.topic = newTopic!),
);
}
}}
>
{session.topic} {session.topic}
</div> </div>
<div className={styles["window-header-sub-title"]}> <div className={styles["window-header-sub-title"]}>

View File

@ -18,6 +18,7 @@ const cn = {
Stop: "停止", Stop: "停止",
Retry: "重试", Retry: "重试",
}, },
Rename: "重命名对话",
Typing: "正在输入…", Typing: "正在输入…",
Input: (submitKey: string) => { Input: (submitKey: string) => {
var inputHints = `输入消息,${submitKey} 发送`; var inputHints = `输入消息,${submitKey} 发送`;
@ -124,7 +125,7 @@ const cn = {
History: (content: string) => History: (content: string) =>
"这是 ai 和用户的历史聊天总结作为前情提要:" + content, "这是 ai 和用户的历史聊天总结作为前情提要:" + content,
Topic: Topic:
"直接返回这句话的简要主题,不要解释,如果没有主题,请直接返回“闲聊”", "使用四到五个字直接返回这句话的简要主题,不要解释、不要标点、不要语气词、不要多余文本,如果没有主题,请直接返回“闲聊”",
Summarize: Summarize:
"简要总结一下你和用户的对话,用作后续的上下文提示 prompt控制在 50 字以内", "简要总结一下你和用户的对话,用作后续的上下文提示 prompt控制在 50 字以内",
}, },

View File

@ -20,6 +20,7 @@ const en: LocaleType = {
Stop: "Stop", Stop: "Stop",
Retry: "Retry", Retry: "Retry",
}, },
Rename: "Rename Chat",
Typing: "Typing…", Typing: "Typing…",
Input: (submitKey: string) => { Input: (submitKey: string) => {
var inputHints = `Type something and press ${submitKey} to send`; var inputHints = `Type something and press ${submitKey} to send`;
@ -129,7 +130,7 @@ const en: LocaleType = {
"This is a summary of the chat history between the AI and the user as a recap: " + "This is a summary of the chat history between the AI and the user as a recap: " +
content, content,
Topic: Topic:
"Provide a brief topic of the sentence without explanation. If there is no topic, return 'Chitchat'.", "Please generate a four to five word title summarizing our conversation without any lead-in, punctuation, quotation marks, periods, symbols, or additional text. Remove enclosing quotation marks.",
Summarize: Summarize:
"Summarize our discussion briefly in 50 characters or less to use as a prompt for future context.", "Summarize our discussion briefly in 50 characters or less to use as a prompt for future context.",
}, },

View File

@ -19,6 +19,7 @@ const tw: LocaleType = {
Stop: "停止", Stop: "停止",
Retry: "重試", Retry: "重試",
}, },
Rename: "重命名對話",
Typing: "正在輸入…", Typing: "正在輸入…",
Input: (submitKey: string) => { Input: (submitKey: string) => {
var inputHints = `輸入訊息後,按下 ${submitKey} 鍵即可發送`; var inputHints = `輸入訊息後,按下 ${submitKey} 鍵即可發送`;

View File

@ -206,6 +206,10 @@ interface ChatStore {
clearAllData: () => void; clearAllData: () => void;
} }
function countMessages(msgs: Message[]) {
return msgs.reduce((pre, cur) => pre + cur.content.length, 0);
}
const LOCAL_KEY = "chat-next-web-store"; const LOCAL_KEY = "chat-next-web-store";
export const useChatStore = create<ChatStore>()( export const useChatStore = create<ChatStore>()(
@ -393,8 +397,12 @@ export const useChatStore = create<ChatStore>()(
summarizeSession() { summarizeSession() {
const session = get().currentSession(); const session = get().currentSession();
if (session.topic === DEFAULT_TOPIC && session.messages.length >= 3) { // should summarize topic after chating more than 50 words
// should summarize topic const SUMMARIZE_MIN_LEN = 50;
if (
session.topic === DEFAULT_TOPIC &&
countMessages(session.messages) >= SUMMARIZE_MIN_LEN
) {
requestWithPrompt(session.messages, Locale.Store.Prompt.Topic).then( requestWithPrompt(session.messages, Locale.Store.Prompt.Topic).then(
(res) => { (res) => {
get().updateCurrentSession( get().updateCurrentSession(
@ -408,10 +416,7 @@ export const useChatStore = create<ChatStore>()(
let toBeSummarizedMsgs = session.messages.slice( let toBeSummarizedMsgs = session.messages.slice(
session.lastSummarizeIndex, session.lastSummarizeIndex,
); );
const historyMsgLength = toBeSummarizedMsgs.reduce( const historyMsgLength = countMessages(toBeSummarizedMsgs);
(pre, cur) => pre + cur.content.length,
0,
);
if (historyMsgLength > 4000) { if (historyMsgLength > 4000) {
toBeSummarizedMsgs = toBeSummarizedMsgs.slice( toBeSummarizedMsgs = toBeSummarizedMsgs.slice(