Skip to content

Commit 45088a3

Browse files
committed
feat: ChatGPTNextWeb#112 add edit chat title
1 parent 08f3c70 commit 45088a3

File tree

7 files changed

+40
-14
lines changed

7 files changed

+40
-14
lines changed

.lintstagedrc.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"./app/**/*.{js,ts,jsx,tsx,json,html,css,scss,md}": [
3-
"eslint --fix",
4-
"prettier --write"
5-
]
6-
}
2+
"./app/**/*.{js,ts,jsx,tsx,json,html,css,md}": [
3+
"eslint --fix",
4+
"prettier --write"
5+
]
6+
}

app/components/home.module.scss

+8
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@
221221
margin-bottom: 100px;
222222
}
223223

224+
.chat-body-title {
225+
cursor: pointer;
226+
227+
&:hover {
228+
text-decoration: underline;
229+
}
230+
}
231+
224232
.chat-message {
225233
display: flex;
226234
flex-direction: row;

app/components/home.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,17 @@ export function Chat(props: {
333333
className={styles["window-header-title"]}
334334
onClick={props?.showSideBar}
335335
>
336-
<div className={styles["window-header-main-title"]}>
336+
<div
337+
className={`${styles["window-header-main-title"]} ${styles["chat-body-title"]}`}
338+
onClick={() => {
339+
const newTopic = prompt(Locale.Chat.Rename, session.topic);
340+
if (newTopic && newTopic !== session.topic) {
341+
chatStore.updateCurrentSession(
342+
(session) => (session.topic = newTopic!),
343+
);
344+
}
345+
}}
346+
>
337347
{session.topic}
338348
</div>
339349
<div className={styles["window-header-sub-title"]}>

app/locales/cn.ts

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

app/locales/en.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const en: LocaleType = {
2020
Stop: "Stop",
2121
Retry: "Retry",
2222
},
23+
Rename: "Rename Chat",
2324
Typing: "Typing…",
2425
Input: (submitKey: string) => {
2526
var inputHints = `Type something and press ${submitKey} to send`;
@@ -129,7 +130,7 @@ const en: LocaleType = {
129130
"This is a summary of the chat history between the AI and the user as a recap: " +
130131
content,
131132
Topic:
132-
"Provide a brief topic of the sentence without explanation. If there is no topic, return 'Chitchat'.",
133+
"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.",
133134
Summarize:
134135
"Summarize our discussion briefly in 50 characters or less to use as a prompt for future context.",
135136
},

app/locales/tw.ts

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

app/store/app.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ interface ChatStore {
206206
clearAllData: () => void;
207207
}
208208

209+
function countMessages(msgs: Message[]) {
210+
return msgs.reduce((pre, cur) => pre + cur.content.length, 0);
211+
}
212+
209213
const LOCAL_KEY = "chat-next-web-store";
210214

211215
export const useChatStore = create<ChatStore>()(
@@ -393,8 +397,12 @@ export const useChatStore = create<ChatStore>()(
393397
summarizeSession() {
394398
const session = get().currentSession();
395399

396-
if (session.topic === DEFAULT_TOPIC && session.messages.length >= 3) {
397-
// should summarize topic
400+
// should summarize topic after chating more than 50 words
401+
const SUMMARIZE_MIN_LEN = 50;
402+
if (
403+
session.topic === DEFAULT_TOPIC &&
404+
countMessages(session.messages) >= SUMMARIZE_MIN_LEN
405+
) {
398406
requestWithPrompt(session.messages, Locale.Store.Prompt.Topic).then(
399407
(res) => {
400408
get().updateCurrentSession(
@@ -408,10 +416,7 @@ export const useChatStore = create<ChatStore>()(
408416
let toBeSummarizedMsgs = session.messages.slice(
409417
session.lastSummarizeIndex,
410418
);
411-
const historyMsgLength = toBeSummarizedMsgs.reduce(
412-
(pre, cur) => pre + cur.content.length,
413-
0,
414-
);
419+
const historyMsgLength = countMessages(toBeSummarizedMsgs);
415420

416421
if (historyMsgLength > 4000) {
417422
toBeSummarizedMsgs = toBeSummarizedMsgs.slice(

0 commit comments

Comments
 (0)