From f9c13df45a5168d6e6389c7a83b14afdc1234b91 Mon Sep 17 00:00:00 2001 From: Yurun Date: Sat, 16 Mar 2024 10:15:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=B8=85=E7=A9=BA=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=20#1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Chat/ApiController/ChatController.php | 14 +++++++++++ server/Module/Chat/Service/ChatService.php | 15 ++++++++++++ web/src/api/index.ts | 6 +++++ web/src/store/modules/chat/index.ts | 10 +++++++- web/src/views/chat/layout/sider/index.vue | 24 ++++++++++++++++--- 5 files changed, 65 insertions(+), 4 deletions(-) diff --git a/server/Module/Chat/ApiController/ChatController.php b/server/Module/Chat/ApiController/ChatController.php index 0d09afe..72beaa7 100644 --- a/server/Module/Chat/ApiController/ChatController.php +++ b/server/Module/Chat/ApiController/ChatController.php @@ -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() diff --git a/server/Module/Chat/Service/ChatService.php b/server/Module/Chat/Service/ChatService.php index 046b808..ea86d56 100644 --- a/server/Module/Chat/Service/ChatService.php +++ b/server/Module/Chat/Service/ChatService.php @@ -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; @@ -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(); diff --git a/web/src/api/index.ts b/web/src/api/index.ts index ea70b00..1d080c9 100644 --- a/web/src/api/index.ts +++ b/web/src/api/index.ts @@ -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) diff --git a/web/src/store/modules/chat/index.ts b/web/src/store/modules/chat/index.ts index 03d167e..fb4b616 100644 --- a/web/src/store/modules/chat/index.ts +++ b/web/src/store/modules/chat/index.ts @@ -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, @@ -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) { diff --git a/web/src/views/chat/layout/sider/index.vue b/web/src/views/chat/layout/sider/index.vue index ab5e507..97f7cd2 100644 --- a/web/src/views/chat/layout/sider/index.vue +++ b/web/src/views/chat/layout/sider/index.vue @@ -1,11 +1,13 @@