Skip to content

Commit 82888b9

Browse files
author
Kerwin
committed
chore: 每个对话上下文关系独立
1 parent 70b3884 commit 82888b9

File tree

10 files changed

+63
-35
lines changed

10 files changed

+63
-35
lines changed

service/src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
updateChat,
3131
updateConfig,
3232
updateRoomPrompt,
33+
updateRoomUsingContext,
3334
updateUserInfo,
3435
updateUserPassword,
3536
verifyUser,
@@ -66,6 +67,7 @@ router.get('/chatrooms', auth, async (req, res) => {
6667
title: r.title,
6768
isEdit: false,
6869
prompt: r.prompt,
70+
usingContext: r.usingContext === undefined ? true : r.usingContext,
6971
})
7072
})
7173
res.send({ status: 'Success', message: null, data: result })
@@ -118,6 +120,22 @@ router.post('/room-prompt', auth, async (req, res) => {
118120
}
119121
})
120122

123+
router.post('/room-context', auth, async (req, res) => {
124+
try {
125+
const userId = req.headers.userId as string
126+
const { using, roomId } = req.body as { using: boolean; roomId: number }
127+
const success = await updateRoomUsingContext(userId, roomId, using)
128+
if (success)
129+
res.send({ status: 'Success', message: 'Saved successfully', data: null })
130+
else
131+
res.send({ status: 'Fail', message: 'Saved Failed', data: null })
132+
}
133+
catch (error) {
134+
console.error(error)
135+
res.send({ status: 'Fail', message: 'Rename error', data: null })
136+
}
137+
})
138+
121139
router.post('/room-delete', auth, async (req, res) => {
122140
try {
123141
const userId = req.headers.userId as string

service/src/storage/model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ export class ChatRoom {
3838
userId: string
3939
title: string
4040
prompt: string
41+
usingContext: boolean
4142
status: Status = Status.Normal
4243
constructor(userId: string, title: string, roomId: number) {
4344
this.userId = userId
4445
this.title = title
4546
this.prompt = undefined
4647
this.roomId = roomId
48+
this.usingContext = true
4749
}
4850
}
4951

service/src/storage/mongo.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ export async function updateRoomPrompt(userId: string, roomId: number, prompt: s
9595
return result.modifiedCount > 0
9696
}
9797

98+
export async function updateRoomUsingContext(userId: string, roomId: number, using: boolean) {
99+
const query = { userId, roomId }
100+
const update = {
101+
$set: {
102+
usingContext: using,
103+
},
104+
}
105+
const result = await roomCol.updateOne(query, update)
106+
return result.modifiedCount > 0
107+
}
108+
98109
export async function getChatRooms(userId: string) {
99110
const cursor = await roomCol.find({ userId, status: { $ne: Status.Deleted } })
100111
const rooms = []

src/api/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ export function fetchUpdateChatRoomPrompt<T = any>(prompt: string, roomId: numbe
148148
})
149149
}
150150

151+
export function fetchUpdateChatRoomUsingContext<T = any>(using: boolean, roomId: number) {
152+
return post<T>({
153+
url: '/room-context',
154+
data: { using, roomId },
155+
})
156+
}
157+
151158
export function fetchDeleteChatRoom<T = any>(roomId: number) {
152159
return post<T>({
153160
url: '/room-delete',

src/store/modules/chat/helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function defaultState(): Chat.ChatState {
77
return {
88
active: uuid,
99
usingContext: true,
10-
history: [{ uuid, title: 'New Chat', isEdit: false }],
10+
history: [{ uuid, title: 'New Chat', isEdit: false, usingContext: true }],
1111
chat: [{ uuid, data: [] }],
1212
}
1313
}

src/store/modules/chat/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineStore } from 'pinia'
22
import { getLocalState, setLocalState } from './helper'
33
import { router } from '@/router'
4-
import { fetchClearChat, fetchCreateChatRoom, fetchDeleteChat, fetchDeleteChatRoom, fetchGetChatHistory, fetchGetChatRooms, fetchRenameChatRoom } from '@/api'
4+
import { fetchClearChat, fetchCreateChatRoom, fetchDeleteChat, fetchDeleteChatRoom, fetchGetChatHistory, fetchGetChatRooms, fetchRenameChatRoom, fetchUpdateChatRoomUsingContext } from '@/api'
55

66
export const useChatStore = defineStore('chat-store', {
77
state: (): Chat.ChatState => getLocalState(),
@@ -39,7 +39,7 @@ export const useChatStore = defineStore('chat-store', {
3939
this.chat.unshift({ uuid: r.uuid, data: [] })
4040
}
4141
if (uuid == null) {
42-
await this.addHistory({ title: 'New Chat', uuid: Date.now(), isEdit: false })
42+
await this.addHistory({ title: 'New Chat', uuid: Date.now(), isEdit: false, usingContext: true })
4343
}
4444
else {
4545
this.active = uuid
@@ -89,8 +89,8 @@ export const useChatStore = defineStore('chat-store', {
8989
}
9090
},
9191

92-
setUsingContext(context: boolean) {
93-
this.usingContext = context
92+
async setUsingContext(context: boolean, roomId: number) {
93+
await fetchUpdateChatRoomUsingContext(context, roomId)
9494
this.recordState()
9595
},
9696

@@ -118,7 +118,7 @@ export const useChatStore = defineStore('chat-store', {
118118
this.chat.splice(index, 1)
119119

120120
if (this.history.length === 0) {
121-
await this.addHistory({ title: 'New Chat', uuid: Date.now(), isEdit: false })
121+
await this.addHistory({ title: 'New Chat', uuid: Date.now(), isEdit: false, usingContext: true })
122122
return
123123
}
124124

@@ -166,7 +166,7 @@ export const useChatStore = defineStore('chat-store', {
166166
if (this.history.length === 0) {
167167
const uuid = Date.now()
168168
fetchCreateChatRoom(chat.text, uuid)
169-
this.history.push({ uuid, title: chat.text, isEdit: false })
169+
this.history.push({ uuid, title: chat.text, isEdit: false, usingContext: true })
170170
this.chat.push({ uuid, data: [chat] })
171171
this.active = uuid
172172
this.recordState()

src/typings/chat.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ declare namespace Chat {
2525
loading?: boolean
2626
all?: boolean
2727
prompt?: string
28+
usingContext: boolean
2829
}
2930

3031
interface ChatState {

src/views/chat/hooks/useUsingContext.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/views/chat/index.vue

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import html2canvas from 'html2canvas'
99
import { Message } from './components'
1010
import { useScroll } from './hooks/useScroll'
1111
import { useChat } from './hooks/useChat'
12-
import { useUsingContext } from './hooks/useUsingContext'
1312
import HeaderComponent from './components/Header/index.vue'
1413
import { HoverButton, SvgIcon } from '@/components/common'
1514
import { useBasicLayout } from '@/hooks/useBasicLayout'
@@ -34,10 +33,11 @@ const chatStore = useChatStore()
3433
const { isMobile } = useBasicLayout()
3534
const { addChat, updateChat, updateChatSome, getChatByUuidAndIndex } = useChat()
3635
const { scrollRef, scrollToBottom, scrollToBottomIfAtBottom, scrollTo } = useScroll()
37-
const { usingContext, toggleUsingContext } = useUsingContext()
3836
3937
const { uuid } = route.params as { uuid: string }
4038
39+
const currentChatHistory = computed(() => chatStore.getChatHistoryByCurrentActive)
40+
const usingContext = computed(() => currentChatHistory?.value?.usingContext ?? true)
4141
const dataSources = computed(() => chatStore.getChatByUuid(+uuid))
4242
const conversationList = computed(() => dataSources.value.filter(item => (!item.inversion && !!item.conversationOptions)))
4343
@@ -513,6 +513,18 @@ async function handleScroll(event: any) {
513513
prevScrollTop = scrollTop
514514
}
515515
516+
async function handleToggleUsingContext() {
517+
if (!currentChatHistory.value)
518+
return
519+
520+
currentChatHistory.value.usingContext = !currentChatHistory.value.usingContext
521+
chatStore.setUsingContext(currentChatHistory.value.usingContext, +uuid)
522+
if (currentChatHistory.value.usingContext)
523+
ms.success(t('chat.turnOnContext'))
524+
else
525+
ms.warning(t('chat.turnOffContext'))
526+
}
527+
516528
// 可优化部分
517529
// 搜索选项计算,这里使用value作为索引项,所以当出现重复value时渲染异常(多项同时出现选中效果)
518530
// 理想状态下其实应该是key作为索引项,但官方的renderOption会出现问题,所以就需要value反renderLabel实现
@@ -577,7 +589,7 @@ onUnmounted(() => {
577589
v-if="isMobile"
578590
:using-context="usingContext"
579591
:show-prompt="showPrompt"
580-
@export="handleExport" @toggle-using-context="toggleUsingContext"
592+
@export="handleExport" @toggle-using-context="handleToggleUsingContext"
581593
@toggle-show-prompt="showPrompt = true"
582594
/>
583595
<main class="flex-1 overflow-hidden">
@@ -642,7 +654,7 @@ onUnmounted(() => {
642654
<IconPrompt class="w-[20px] m-auto" />
643655
</span>
644656
</HoverButton>
645-
<HoverButton v-if="!isMobile" @click="toggleUsingContext">
657+
<HoverButton v-if="!isMobile" @click="handleToggleUsingContext">
646658
<span class="text-xl" :class="{ 'text-[#4b9e5f]': usingContext, 'text-[#a8071a]': !usingContext }">
647659
<SvgIcon icon="ri:chat-history-line" />
648660
</span>

src/views/chat/layout/sider/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const show = ref(false)
1818
const collapsed = computed(() => appStore.siderCollapsed)
1919
2020
async function handleAdd() {
21-
await chatStore.addHistory({ title: 'New Chat', uuid: Date.now(), isEdit: false })
21+
await chatStore.addHistory({ title: 'New Chat', uuid: Date.now(), isEdit: false, usingContext: true })
2222
if (isMobile.value)
2323
appStore.setSiderCollapsed(true)
2424
}

0 commit comments

Comments
 (0)