Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor chat conversation component to improve modularity and readab… #526

Merged
merged 5 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading