Skip to content

Commit

Permalink
支持清空历史 #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Mar 16, 2024
1 parent aecc386 commit f9c13df
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
14 changes: 14 additions & 0 deletions server/Module/Chat/ApiController/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ public function delete(string $id)
$this->chatService->delete($id, $memberSession->getIntMemberId(), SessionType::CHAT);
}

/**
* @return mixed
*/
#[
Action,
Route(method: RequestMethod::POST),
LoginRequired()
]
public function clear()
{
$memberSession = MemberUtil::getMemberSession();
$this->chatService->clear($memberSession->getIntMemberId(), SessionType::CHAT);
}

#[
Action,
LoginRequired()
Expand Down
15 changes: 15 additions & 0 deletions server/Module/Chat/Service/ChatService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use app\Util\TokensUtil;
use Imi\Aop\Annotation\Inject;
use Imi\Db\Annotation\Transaction;
use Imi\Db\Db;
use Imi\Db\Mysql\Consts\LogicalOperator;
use Imi\Db\Query\Where\Where;
use Imi\Log\Log;
Expand Down Expand Up @@ -311,6 +312,20 @@ public function delete(string|int $id, int $memberId = 0, int $type = 0, int $op
}
}

#[Transaction()]
public function clear(int $memberId = 0, int $type = 0): void
{
Db::exec(<<<'SQL'
DELETE tb_chat_session, tb_chat_message
FROM
tb_chat_session
JOIN tb_chat_message ON tb_chat_message.session_id = tb_chat_session.id
WHERE
`member_id` = ?
AND `type` = ?
SQL, [$memberId, $type]);
}

public function appendMessage(int $sessionId, string $role, array|object $config, int $tokens, string $message, int $beginTime, int $completeTime, string $ip): ChatMessage
{
$record = ChatMessage::newInstance();
Expand Down
6 changes: 6 additions & 0 deletions web/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ export function deleteSession(
})
}

export function clearSession() {
return post({
url: '/chat/clear',
})
}

export function decodeChatSessionSecureFields(data: any) {
data.title = decodeSecureField(data.title)
data.prompt = decodeSecureField(data.prompt)
Expand Down
10 changes: 9 additions & 1 deletion web/src/store/modules/chat/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineStore } from 'pinia'
import { getLocalState } from './helper'
import { router } from '@/router'
import { deleteSession, sessionList } from '@/api'
import { clearSession, deleteSession, sessionList } from '@/api'

export const enum QAStatus {
ASK = 1,
Expand Down Expand Up @@ -143,6 +143,14 @@ export const useChatStore = defineStore('chat-store', {
}
},

async clearHistory() {
await clearSession()
this.history = []
this.chat = []
this.active = null
this.reloadRoute(undefined, null)
},

async deleteHistoryById(id: string, reload = true) {
const index = this.history.findIndex(item => item.id === id)
if (index !== -1) {
Expand Down
24 changes: 21 additions & 3 deletions web/src/views/chat/layout/sider/index.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<script setup lang='tsx'>
import type { CSSProperties } from 'vue'
import { computed, ref, watch } from 'vue'
import { NButton, NLayoutSider } from 'naive-ui'
import { NButton, NIcon, NLayoutSider, useDialog } from 'naive-ui'
import { TrashOutline } from '@vicons/ionicons5'
import List from './List.vue'
import { QAStatus, useAppStore, useChatStore } from '@/store'
import { useBasicLayout } from '@/hooks/useBasicLayout'
import { PromptStore } from '@/components/common'
import { t } from '@/locales'
const appStore = useAppStore()
const chatStore = useChatStore()
Expand All @@ -14,6 +16,7 @@ const { isMobile } = useBasicLayout()
const show = ref(false)
const collapsed = computed(() => appStore.siderCollapsed)
const dialog = useDialog()
function handleAdd() {
chatStore.deleteHistoryById('', false)
Expand All @@ -30,6 +33,18 @@ function handleAdd() {
appStore.setSiderCollapsed(true)
}
function handleClear() {
dialog.warning({
title: '询问',
content: '是否清空历史记录?',
positiveText: t('common.yes'),
negativeText: t('common.no'),
onPositiveClick: async () => {
chatStore.clearHistory()
},
})
}
function handleUpdateCollapsed() {
appStore.setSiderCollapsed(!collapsed.value)
}
Expand Down Expand Up @@ -79,10 +94,13 @@ watch(
>
<div class="flex flex-col h-full" :style="mobileSafeArea">
<main class="flex flex-col flex-1 min-h-0">
<div class="p-4">
<NButton dashed block @click="handleAdd">
<div class="p-4 flex flex-row">
<NButton dashed class="flex-1 !mr-1" @click="handleAdd">
{{ $t('chat.newChatButton') }}
</NButton>
<NButton dashed title="清空历史" @click="handleClear">
<NIcon class="align-middle" :component="TrashOutline" size="14" />
</NButton>
</div>
<div class="flex-1 min-h-0 pb-4 overflow-hidden">
<List />
Expand Down

0 comments on commit f9c13df

Please sign in to comment.