Skip to content

Commit

Permalink
Refactor chat conversation component to improve modularity and readab… (
Browse files Browse the repository at this point in the history
#526)

* 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.
  • Loading branch information
swuecho authored Sep 8, 2024
1 parent b4baae3 commit a81c087
Show file tree
Hide file tree
Showing 5 changed files with 670 additions and 645 deletions.
15 changes: 5 additions & 10 deletions web/src/store/modules/chat/helper.ts
Original file line number Diff line number Diff line change
@@ -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<string>, number?] = [keys]
if (need_length) {
const keys_length = keys.length
data.push(keys_length)
}
return data
return includeLength ? [keys, keys.length] as const : [keys]
}
17 changes: 7 additions & 10 deletions web/src/store/modules/chat/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -230,7 +229,7 @@ export const useChatStore = defineStore('chat-store', {
index: number,
chat: Partial<Chat.Message>,
) {
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 }
Expand All @@ -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]]
Expand All @@ -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]) {
Expand Down
Loading

0 comments on commit a81c087

Please sign in to comment.