From a81c087a9366d11be593206949447bde9bffd8a2 Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Sun, 8 Sep 2024 19:58:23 +0800 Subject: [PATCH] =?UTF-8?q?Refactor=20chat=20conversation=20component=20to?= =?UTF-8?q?=20improve=20modularity=20and=20readab=E2=80=A6=20(#526)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refactor chat conversation component to improve modularity and readability. * Remove deprecated `conversation.vue` and update import path in `index.vue` * Refactor import paths in Conversation.vue and MessageList.vue to use absolute paths for consistency. * Refactor Conversation component to accept sessionUuid as prop instead of using route params. * Refactor chat helper functions and update related store methods. --- web/src/store/modules/chat/helper.ts | 15 +- web/src/store/modules/chat/index.ts | 17 +- .../views/chat/components/Conversation.vue | 567 ++++++++++++++++ web/src/views/chat/components/MessageList.vue | 87 +++ web/src/views/chat/index.vue | 629 +----------------- 5 files changed, 670 insertions(+), 645 deletions(-) create mode 100644 web/src/views/chat/components/Conversation.vue create mode 100644 web/src/views/chat/components/MessageList.vue diff --git a/web/src/store/modules/chat/helper.ts b/web/src/store/modules/chat/helper.ts index f9d588cb..1dc05b05 100644 --- a/web/src/store/modules/chat/helper.ts +++ b/web/src/store/modules/chat/helper.ts @@ -1,19 +1,14 @@ const default_chat_data: Chat.ChatState = { - active: null, - history: [], - chat: {}, + active: null, // uuid | null + history: [], // Chat.Session[] + chat: {}, // { [key: string]: Chat.ChatMessage[] } } export function getLocalState(): Chat.ChatState { return default_chat_data } -export function check_chat(chat: Chat.ChatState['chat'], need_length = true) { +export function getChatKeys(chat: Chat.ChatState['chat'], includeLength = true) { const keys = Object.keys(chat) - const data: [Array, number?] = [keys] - if (need_length) { - const keys_length = keys.length - data.push(keys_length) - } - return data + return includeLength ? [keys, keys.length] as const : [keys] } diff --git a/web/src/store/modules/chat/index.ts b/web/src/store/modules/chat/index.ts index ccb43501..f3d9269e 100644 --- a/web/src/store/modules/chat/index.ts +++ b/web/src/store/modules/chat/index.ts @@ -1,5 +1,5 @@ import { defineStore } from 'pinia' -import { check_chat, getLocalState, } from './helper' +import { getChatKeys, getLocalState, } from './helper' import { router } from '@/router' import { clearSessionChatMessages, @@ -159,7 +159,7 @@ export const useChatStore = defineStore('chat-store', { }, getChatByUuidAndIndex(uuid: string, index: number) { - const [keys, keys_length] = check_chat(this.chat) + const [keys, keys_length] = getChatKeys(this.chat) if (!uuid) { if (keys_length) return this.chat[keys[0]][index] @@ -173,7 +173,7 @@ export const useChatStore = defineStore('chat-store', { async addChatByUuid(uuid: string, chat: Chat.Message) { const new_chat_text = t('chat.new') - const [keys] = check_chat(this.chat, false) + const [keys] = getChatKeys(this.chat, false) if (!uuid) { if (this.history.length === 0) { const default_model_parameters = await getChatSessionDefault(new_chat_text) @@ -210,8 +210,7 @@ export const useChatStore = defineStore('chat-store', { }, async updateChatByUuid(uuid: string, index: number, chat: Chat.Message) { - // TODO: sync with server - const [keys, keys_length] = check_chat(this.chat) + const [keys, keys_length] = getChatKeys(this.chat) if (!uuid) { if (keys_length) { this.chat[keys[0]][index] = chat @@ -230,7 +229,7 @@ export const useChatStore = defineStore('chat-store', { index: number, chat: Partial, ) { - const [keys, keys_length] = check_chat(this.chat) + const [keys, keys_length] = getChatKeys(this.chat) if (!uuid) { if (keys_length) { this.chat[keys[0]][index] = { ...this.chat[keys[0]][index], ...chat } @@ -248,7 +247,7 @@ export const useChatStore = defineStore('chat-store', { }, async deleteChatByUuid(uuid: string, index: number) { - const [keys, keys_length] = check_chat(this.chat) + const [keys, keys_length] = getChatKeys(this.chat) if (!uuid) { if (keys_length) { const chatData = this.chat[keys[0]] @@ -272,15 +271,13 @@ export const useChatStore = defineStore('chat-store', { clearChatByUuid(uuid: string) { // does this every happen? - const [keys, keys_length] = check_chat(this.chat) + const [keys, keys_length] = getChatKeys(this.chat) if (!uuid) { if (keys_length) { this.chat[keys[0]] = [] } return } - - // const index = this.chat.findIndex(item => item.uuid === uuid) if (keys.includes(uuid)) { const data: Chat.Message[] = [] for (const chat of this.chat[uuid]) { diff --git a/web/src/views/chat/components/Conversation.vue b/web/src/views/chat/components/Conversation.vue new file mode 100644 index 00000000..9baed463 --- /dev/null +++ b/web/src/views/chat/components/Conversation.vue @@ -0,0 +1,567 @@ + + + \ No newline at end of file diff --git a/web/src/views/chat/components/MessageList.vue b/web/src/views/chat/components/MessageList.vue new file mode 100644 index 00000000..61634b58 --- /dev/null +++ b/web/src/views/chat/components/MessageList.vue @@ -0,0 +1,87 @@ + + + diff --git a/web/src/views/chat/index.vue b/web/src/views/chat/index.vue index 568fb704..25c9895b 100644 --- a/web/src/views/chat/index.vue +++ b/web/src/views/chat/index.vue @@ -1,635 +1,14 @@ \ No newline at end of file