Skip to content

Commit 97a756d

Browse files
committed
feat: add web search switch button
Signed-off-by: Bob Du <i@bobdu.cc>
1 parent 6a978e0 commit 97a756d

File tree

10 files changed

+97
-7
lines changed

10 files changed

+97
-7
lines changed

service/src/routes/room.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
renameChatRoom,
1010
updateRoomChatModel,
1111
updateRoomPrompt,
12+
updateRoomSearchEnabled,
1213
updateRoomUsingContext,
1314
} from '../storage/mongo'
1415

@@ -27,6 +28,7 @@ router.get('/chatrooms', auth, async (req, res) => {
2728
prompt: r.prompt,
2829
usingContext: r.usingContext === undefined ? true : r.usingContext,
2930
chatModel: r.chatModel,
31+
searchEnabled: !!r.searchEnabled,
3032
})
3133
})
3234
res.send({ status: 'Success', message: null, data: result })
@@ -135,6 +137,22 @@ router.post('/room-chatmodel', auth, async (req, res) => {
135137
}
136138
})
137139

140+
router.post('/room-search-enabled', auth, async (req, res) => {
141+
try {
142+
const userId = req.headers.userId as string
143+
const { searchEnabled, roomId } = req.body as { searchEnabled: boolean; roomId: number }
144+
const success = await updateRoomSearchEnabled(userId, roomId, searchEnabled)
145+
if (success)
146+
res.send({ status: 'Success', message: 'Saved successfully', data: null })
147+
else
148+
res.send({ status: 'Fail', message: 'Saved Failed', data: null })
149+
}
150+
catch (error) {
151+
console.error(error)
152+
res.send({ status: 'Fail', message: 'Update error', data: null })
153+
}
154+
})
155+
138156
router.post('/room-context', auth, async (req, res) => {
139157
try {
140158
const userId = req.headers.userId as string

service/src/storage/model.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,16 @@ export class ChatRoom {
8181
prompt: string
8282
usingContext: boolean
8383
status: Status = Status.Normal
84-
// only access token used
85-
accountId?: string
8684
chatModel: string
87-
constructor(userId: string, title: string, roomId: number, chatModel: string) {
85+
searchEnabled: boolean
86+
constructor(userId: string, title: string, roomId: number, chatModel: string, searchEnabled: boolean) {
8887
this.userId = userId
8988
this.title = title
9089
this.prompt = undefined
9190
this.roomId = roomId
9291
this.usingContext = true
93-
this.accountId = null
9492
this.chatModel = chatModel
93+
this.searchEnabled = searchEnabled
9594
}
9695
}
9796

service/src/storage/mongo.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export async function insertChatUsage(userId: ObjectId,
127127
}
128128

129129
export async function createChatRoom(userId: string, title: string, roomId: number, chatModel: string) {
130-
const room = new ChatRoom(userId, title, roomId, chatModel)
130+
const room = new ChatRoom(userId, title, roomId, chatModel, false)
131131
await roomCol.insertOne(room)
132132
return room
133133
}
@@ -182,6 +182,17 @@ export async function updateRoomChatModel(userId: string, roomId: number, chatMo
182182
return result.modifiedCount > 0
183183
}
184184

185+
export async function updateRoomSearchEnabled(userId: string, roomId: number, searchEnabled: boolean) {
186+
const query = { userId, roomId }
187+
const update = {
188+
$set: {
189+
searchEnabled,
190+
},
191+
}
192+
const result = await roomCol.updateOne(query, update)
193+
return result.modifiedCount > 0
194+
}
195+
185196
export async function getChatRooms(userId: string) {
186197
const cursor = roomCol.find({ userId, status: { $ne: Status.Deleted } })
187198
const rooms = []

src/api/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,13 @@ export function fetchUpdateChatRoomUsingContext<T = any>(using: boolean, roomId:
271271
})
272272
}
273273

274+
export function fetchUpdateChatRoomSearchEnabled<T = any>(searchEnabled: boolean, roomId: number) {
275+
return post<T>({
276+
url: '/room-search-enabled',
277+
data: { searchEnabled, roomId },
278+
})
279+
}
280+
274281
export function fetchDeleteChatRoom<T = any>(roomId: number) {
275282
return post<T>({
276283
url: '/room-delete',

src/locales/en-US.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,16 @@ export default {
5555
usingContext: 'Context Mode',
5656
turnOnContext: 'In the current mode, sending messages will carry previous chat records.',
5757
turnOffContext: 'In the current mode, sending messages will not carry previous chat records.',
58+
turnOnSearch: 'Web search has been enabled for this chat.',
59+
turnOffSearch: 'Web search has been disabled for this chat.',
5860
clickTurnOnContext: 'Click to enable sending messages will carry previous chat records.',
5961
clickTurnOffContext: 'Click to disable sending messages will carry previous chat records.',
62+
clickTurnOnSearch: 'Click to enable web search for this chat.',
63+
clickTurnOffSearch: 'Click to disable web search for this chat.',
6064
showOnContext: 'Include context',
6165
showOffContext: 'Not include context',
66+
searchEnabled: 'Search enabled',
67+
searchDisabled: 'Search disabled',
6268
deleteMessage: 'Delete Message',
6369
deleteMessageConfirm: 'Are you sure to delete this message?',
6470
deleteHistoryConfirm: 'Are you sure to clear this history?',

src/locales/zh-CN.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,16 @@ export default {
5555
usingContext: '上下文模式',
5656
turnOnContext: '当前模式下, 发送消息会携带之前的聊天记录',
5757
turnOffContext: '当前模式下, 发送消息不会携带之前的聊天记录',
58+
turnOnSearch: '已开启网络搜索功能',
59+
turnOffSearch: '已关闭网络搜索功能',
5860
clickTurnOnContext: '点击开启包含上下文',
5961
clickTurnOffContext: '点击停止包含上下文',
62+
clickTurnOnSearch: '点击开启网络搜索功能',
63+
clickTurnOffSearch: '点击关闭网络搜索功能',
6064
showOnContext: '包含上下文',
6165
showOffContext: '不含上下文',
66+
searchEnabled: '搜索已开启',
67+
searchDisabled: '搜索已关闭',
6268
deleteMessage: '删除消息',
6369
deleteMessageConfirm: '是否删除此消息?',
6470
deleteHistoryConfirm: '确定删除此记录?',

src/store/modules/chat/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
fetchGetChatRooms,
1212
fetchRenameChatRoom,
1313
fetchUpdateChatRoomChatModel,
14+
fetchUpdateChatRoomSearchEnabled,
1415
fetchUpdateChatRoomUsingContext,
1516
fetchUpdateUserChatModel,
1617
} from '@/api'
@@ -115,6 +116,15 @@ export const useChatStore = defineStore('chat-store', {
115116
await fetchUpdateUserChatModel(chatModel)
116117
},
117118

119+
async setChatSearchEnabled(searchEnabled: boolean, roomId: number) {
120+
const index = this.history.findIndex(item => item.uuid === this.active)
121+
if (index !== -1) {
122+
this.history[index].searchEnabled = searchEnabled
123+
await fetchUpdateChatRoomSearchEnabled(searchEnabled, roomId)
124+
this.recordState()
125+
}
126+
},
127+
118128
async addHistory(history: Chat.History, chatData: Chat.Chat[] = []) {
119129
await fetchCreateChatRoom(history.title, history.uuid, history.chatModel)
120130
this.history.unshift(history)

src/typings/chat.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ declare namespace Chat {
2929
prompt?: string
3030
usingContext: boolean
3131
chatModel?: string
32+
searchEnabled?: boolean
3233
}
3334

3435
interface ChatState {

src/views/chat/components/Header/index.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import IconPrompt from '@/icons/Prompt.vue'
77
interface Props {
88
usingContext: boolean
99
showPrompt: boolean
10+
searchEnabled?: boolean
1011
}
1112
1213
interface Emit {
1314
(ev: 'export'): void
1415
(ev: 'toggleUsingContext'): void
1516
(ev: 'toggleShowPrompt'): void
17+
(ev: 'toggleSearchEnabled'): void
1618
}
1719
1820
defineProps<Props>()
@@ -43,6 +45,10 @@ function toggleUsingContext() {
4345
emit('toggleUsingContext')
4446
}
4547
48+
function toggleSearchEnabled() {
49+
emit('toggleSearchEnabled')
50+
}
51+
4652
function handleShowPrompt() {
4753
emit('toggleShowPrompt')
4854
}
@@ -80,6 +86,12 @@ function handleShowPrompt() {
8086
</span>
8187
<span style="margin-left:.25em">{{ usingContext ? $t('chat.showOnContext') : $t('chat.showOffContext') }}</span>
8288
</HoverButton>
89+
<HoverButton :class="{ 'text-[#4b9e5f]': searchEnabled, 'text-[#a8071a]': !searchEnabled }" @click="toggleSearchEnabled">
90+
<span class="text-xl">
91+
<SvgIcon icon="mdi:web" />
92+
</span>
93+
<span style="margin-left:.25em">{{ searchEnabled ? $t('chat.searchEnabled') : $t('chat.searchDisabled') }}</span>
94+
</HoverButton>
8395
<HoverButton @click="handleExport">
8496
<span class="text-xl text-[#4f555e] dark:text-white">
8597
<SvgIcon icon="ri:download-2-line" />

src/views/chat/index.vue

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,19 @@ async function handleScroll(event: any) {
552552
prevScrollTop = scrollTop
553553
}
554554
555+
async function handleToggleSearchEnabled() {
556+
if (!currentChatHistory.value)
557+
return
558+
559+
const searchEnabled = currentChatHistory.value.searchEnabled ?? false
560+
currentChatHistory.value.searchEnabled = !searchEnabled
561+
await chatStore.setChatSearchEnabled(!searchEnabled, +uuid)
562+
if (currentChatHistory.value.searchEnabled)
563+
ms.success(t('chat.turnOnSearch'))
564+
else
565+
ms.warning(t('chat.turnOffSearch'))
566+
}
567+
555568
async function handleToggleUsingContext() {
556569
if (!currentChatHistory.value)
557570
return
@@ -662,7 +675,10 @@ onUnmounted(() => {
662675
v-if="isMobile"
663676
:using-context="usingContext"
664677
:show-prompt="showPrompt"
665-
@export="handleExport" @toggle-using-context="handleToggleUsingContext"
678+
:search-enabled="currentChatHistory?.searchEnabled"
679+
@export="handleExport"
680+
@toggle-using-context="handleToggleUsingContext"
681+
@toggle-search-enabled="handleToggleSearchEnabled"
666682
@toggle-show-prompt="showPrompt = true"
667683
/>
668684
<main class="flex-1 overflow-hidden">
@@ -767,7 +783,6 @@ onUnmounted(() => {
767783
<span class="text-xl">
768784
<SvgIcon icon="ri:chat-history-line" />
769785
</span>
770-
<!-- <span style="margin-left:.25em">{{ usingContext ? '包含上下文' : '不含上下文' }}</span> -->
771786
</HoverButton>
772787
<NSelect
773788
style="width: 250px"
@@ -776,6 +791,11 @@ onUnmounted(() => {
776791
:disabled="!!authStore.session?.auth && !authStore.token && !authStore.session?.authProxyEnabled"
777792
@update-value="(val) => handleSyncChatModel(val)"
778793
/>
794+
<HoverButton v-if="!isMobile" :tooltip="currentChatHistory?.searchEnabled ? $t('chat.clickTurnOffSearch') : $t('chat.clickTurnOnSearch')" :class="{ 'text-[#4b9e5f]': currentChatHistory?.searchEnabled, 'text-[#a8071a]': !currentChatHistory?.searchEnabled }" @click="handleToggleSearchEnabled">
795+
<span class="text-xl">
796+
<SvgIcon icon="mdi:web" />
797+
</span>
798+
</HoverButton>
779799
<NSlider v-model:value="userStore.userInfo.advanced.maxContextCount" :max="100" :min="0" :step="1" style="width: 88px" :format-tooltip="formatTooltip" @update:value="() => { userStore.updateSetting(false) }" />
780800
</div>
781801
<div class="flex items-center justify-between space-x-2">

0 commit comments

Comments
 (0)