From 45088a3e0658beac56251ff2d4cebc8dc2c5becc Mon Sep 17 00:00:00 2001 From: Yifei Zhang Date: Wed, 29 Mar 2023 16:02:50 +0000 Subject: [PATCH] feat: #112 add edit chat title --- .lintstagedrc.json | 10 +++++----- app/components/home.module.scss | 8 ++++++++ app/components/home.tsx | 12 +++++++++++- app/locales/cn.ts | 3 ++- app/locales/en.ts | 3 ++- app/locales/tw.ts | 1 + app/store/app.ts | 17 +++++++++++------ 7 files changed, 40 insertions(+), 14 deletions(-) diff --git a/.lintstagedrc.json b/.lintstagedrc.json index 023bf16ab..58784bad8 100644 --- a/.lintstagedrc.json +++ b/.lintstagedrc.json @@ -1,6 +1,6 @@ { - "./app/**/*.{js,ts,jsx,tsx,json,html,css,scss,md}": [ - "eslint --fix", - "prettier --write" - ] -} \ No newline at end of file + "./app/**/*.{js,ts,jsx,tsx,json,html,css,md}": [ + "eslint --fix", + "prettier --write" + ] +} diff --git a/app/components/home.module.scss b/app/components/home.module.scss index fb96bd451..87231feee 100644 --- a/app/components/home.module.scss +++ b/app/components/home.module.scss @@ -221,6 +221,14 @@ margin-bottom: 100px; } +.chat-body-title { + cursor: pointer; + + &:hover { + text-decoration: underline; + } +} + .chat-message { display: flex; flex-direction: row; diff --git a/app/components/home.tsx b/app/components/home.tsx index de2cc8fe7..847b6cf46 100644 --- a/app/components/home.tsx +++ b/app/components/home.tsx @@ -333,7 +333,17 @@ export function Chat(props: { className={styles["window-header-title"]} onClick={props?.showSideBar} > -
+
{ + const newTopic = prompt(Locale.Chat.Rename, session.topic); + if (newTopic && newTopic !== session.topic) { + chatStore.updateCurrentSession( + (session) => (session.topic = newTopic!), + ); + } + }} + > {session.topic}
diff --git a/app/locales/cn.ts b/app/locales/cn.ts index 76f6a770c..e9cbad9e1 100644 --- a/app/locales/cn.ts +++ b/app/locales/cn.ts @@ -18,6 +18,7 @@ const cn = { Stop: "停止", Retry: "重试", }, + Rename: "重命名对话", Typing: "正在输入…", Input: (submitKey: string) => { var inputHints = `输入消息,${submitKey} 发送`; @@ -124,7 +125,7 @@ const cn = { History: (content: string) => "这是 ai 和用户的历史聊天总结作为前情提要:" + content, Topic: - "直接返回这句话的简要主题,不要解释,如果没有主题,请直接返回“闲聊”", + "使用四到五个字直接返回这句话的简要主题,不要解释、不要标点、不要语气词、不要多余文本,如果没有主题,请直接返回“闲聊”", Summarize: "简要总结一下你和用户的对话,用作后续的上下文提示 prompt,控制在 50 字以内", }, diff --git a/app/locales/en.ts b/app/locales/en.ts index d21679ab9..f31badd0c 100644 --- a/app/locales/en.ts +++ b/app/locales/en.ts @@ -20,6 +20,7 @@ const en: LocaleType = { Stop: "Stop", Retry: "Retry", }, + Rename: "Rename Chat", Typing: "Typing…", Input: (submitKey: string) => { 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: " + content, 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 our discussion briefly in 50 characters or less to use as a prompt for future context.", }, diff --git a/app/locales/tw.ts b/app/locales/tw.ts index 63312eb4d..b78e1b834 100644 --- a/app/locales/tw.ts +++ b/app/locales/tw.ts @@ -19,6 +19,7 @@ const tw: LocaleType = { Stop: "停止", Retry: "重試", }, + Rename: "重命名對話", Typing: "正在輸入…", Input: (submitKey: string) => { var inputHints = `輸入訊息後,按下 ${submitKey} 鍵即可發送`; diff --git a/app/store/app.ts b/app/store/app.ts index 8a978c0a4..118e9ed6c 100644 --- a/app/store/app.ts +++ b/app/store/app.ts @@ -206,6 +206,10 @@ interface ChatStore { clearAllData: () => void; } +function countMessages(msgs: Message[]) { + return msgs.reduce((pre, cur) => pre + cur.content.length, 0); +} + const LOCAL_KEY = "chat-next-web-store"; export const useChatStore = create()( @@ -393,8 +397,12 @@ export const useChatStore = create()( summarizeSession() { const session = get().currentSession(); - if (session.topic === DEFAULT_TOPIC && session.messages.length >= 3) { - // should summarize topic + // should summarize topic after chating more than 50 words + const SUMMARIZE_MIN_LEN = 50; + if ( + session.topic === DEFAULT_TOPIC && + countMessages(session.messages) >= SUMMARIZE_MIN_LEN + ) { requestWithPrompt(session.messages, Locale.Store.Prompt.Topic).then( (res) => { get().updateCurrentSession( @@ -408,10 +416,7 @@ export const useChatStore = create()( let toBeSummarizedMsgs = session.messages.slice( session.lastSummarizeIndex, ); - const historyMsgLength = toBeSummarizedMsgs.reduce( - (pre, cur) => pre + cur.content.length, - 0, - ); + const historyMsgLength = countMessages(toBeSummarizedMsgs); if (historyMsgLength > 4000) { toBeSummarizedMsgs = toBeSummarizedMsgs.slice(