Skip to content

Commit 1250fb3

Browse files
committed
feat: 历史消息清理按钮
1 parent fd0256c commit 1250fb3

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

src/devtools/App.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import SettingsIcon from '@mui/icons-material/Settings';
1919
import AddIcon from '@mui/icons-material/Add';
2020
import InfoOutlinedIcon from '@mui/icons-material/InfoOutlined';
2121
import * as prompts from './prompts'
22+
import CleaningServicesIcon from '@mui/icons-material/CleaningServices';
23+
import CleanWidnow from './CleanWindow';
2224

2325
const { useEffect, useState } = React
2426

@@ -36,7 +38,7 @@ function App() {
3638
// 是否展示应用更新提示
3739
const [needCheckUpdate, setNeedCheckUpdate] = useState(true)
3840

39-
const [scrollToMsg, setScrollToMsg] = useState<{msgId: string, smooth?: boolean}>(null)
41+
const [scrollToMsg, setScrollToMsg] = useState<{ msgId: string, smooth?: boolean }>(null)
4042
useEffect(() => {
4143
if (!scrollToMsg) {
4244
return
@@ -70,7 +72,7 @@ function App() {
7072
return
7173
}
7274
const last = store.currentSession.messages[store.currentSession.messages.length - 1]
73-
setScrollToMsg({msgId: last.id, smooth: false})
75+
setScrollToMsg({ msgId: last.id, smooth: false })
7476
}, [store.currentSession])
7577

7678
// 会话名称自动生成
@@ -82,6 +84,8 @@ function App() {
8284

8385
const [configureChatConfig, setConfigureChatConfig] = React.useState<Session | null>(null);
8486

87+
const [sessionClean, setSessionClean] = React.useState<Session | null>(null);
88+
8589
const generateName = async (session: Session) => {
8690
client.replay(
8791
store.settings.openaiKey,
@@ -114,7 +118,7 @@ function App() {
114118
}
115119
}
116120
store.updateChatSession(session)
117-
setScrollToMsg({msgId: targetMsg.id, smooth: false})
121+
setScrollToMsg({ msgId: targetMsg.id, smooth: false })
118122
},
119123
(err) => {
120124
for (let i = 0; i < session.messages.length; i++) {
@@ -267,9 +271,14 @@ function App() {
267271
<IconButton edge="start" color="inherit" aria-label="menu" sx={{ mr: 2 }}>
268272
<ChatBubbleOutlineOutlinedIcon />
269273
</IconButton>
270-
<Typography variant="h6" color="inherit" component="div" noWrap>
274+
<Typography variant="h6" color="inherit" component="div" noWrap sx={{ flexGrow: 1 }}>
271275
{store.currentSession.name}
272276
</Typography>
277+
<IconButton edge="start" color="inherit" aria-label="menu" sx={{ mr: 2 }}
278+
onClick={() => setSessionClean(store.currentSession)}
279+
>
280+
<CleaningServicesIcon />
281+
</IconButton>
273282
</Toolbar>
274283
<Divider />
275284
<List
@@ -318,7 +327,7 @@ function App() {
318327
store.currentSession.messages = [...store.currentSession.messages, newUserMsg, newAssistantMsg]
319328
store.updateChatSession(store.currentSession)
320329
generate(store.currentSession, promptsMsgs, newAssistantMsg)
321-
setScrollToMsg({msgId: newAssistantMsg.id, smooth: true})
330+
setScrollToMsg({ msgId: newAssistantMsg.id, smooth: true })
322331
}} />
323332
</Box>
324333
</Stack>
@@ -344,6 +353,18 @@ function App() {
344353
/>
345354
)
346355
}
356+
{
357+
sessionClean !== null && (
358+
<CleanWidnow open={sessionClean !== null}
359+
session={sessionClean}
360+
save={(session) => {
361+
store.updateChatSession(session)
362+
setSessionClean(null)
363+
}}
364+
close={() => setSessionClean(null)}
365+
/>
366+
)
367+
}
347368
{
348369
store.toasts.map((toast) => (
349370
<Snackbar

src/devtools/CleanWindow.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import './App.css';
2+
import {
3+
Button, Dialog, DialogContent, DialogActions, DialogTitle, DialogContentText,
4+
} from '@mui/material';
5+
import { Session } from './types'
6+
7+
interface Props {
8+
open: boolean
9+
session: Session
10+
save(session: Session): void
11+
close(): void
12+
}
13+
14+
export default function CleanWindow(props: Props) {
15+
const clean = () => {
16+
const messages = props.session.messages.filter(msg => msg.role === 'system')
17+
props.save({ ...props.session, messages })
18+
}
19+
return (
20+
<Dialog open={props.open} onClose={props.close}>
21+
<DialogTitle>Cleaning</DialogTitle>
22+
<DialogContent>
23+
<DialogContentText>
24+
This action will permanently delete all non-system messages in [{props.session.name}].
25+
Are you sure you want to continue?
26+
</DialogContentText>
27+
</DialogContent>
28+
<DialogActions>
29+
<Button onClick={props.close}>Cancel</Button>
30+
<Button onClick={clean} color="error">Clean it up</Button>
31+
</DialogActions>
32+
</Dialog>
33+
)
34+
}

0 commit comments

Comments
 (0)