Skip to content

Commit

Permalink
Refactor 20240908 (#527)
Browse files Browse the repository at this point in the history
* Remove commented-out code and unnecessary comments in chat store module.

* Sort chat sessions by ID in descending order and sync sessions in chat store.

* Make `addChatSession` method asynchronous and await its calls in chat store and sider component.
  • Loading branch information
swuecho authored Sep 8, 2024
1 parent a81c087 commit f7b443c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 25 deletions.
2 changes: 1 addition & 1 deletion api/sqlc/queries/chat_session.sql
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ returning *;
SELECT cs.*
FROM chat_session cs
WHERE cs.user_id = $1 and cs.active = true
ORDER BY cs.id;
ORDER BY cs.id DESC;

-- name: HasChatSessionPermission :one
SELECT COUNT(*) > 0 as has_permission
Expand Down
2 changes: 1 addition & 1 deletion api/sqlc_queries/chat_session.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 7 additions & 22 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 { getChatKeys, getLocalState, } from './helper'
import { getChatKeys, getLocalState } from './helper'
import { router } from '@/router'
import {
clearSessionChatMessages,
Expand Down Expand Up @@ -56,16 +56,12 @@ export const useChatStore = defineStore('chat-store', {
await router.push({ name: 'Chat', params: { uuid } })
},


async syncChatSessions() {
const sessions = await getChatSessionsByUser()
this.history = []
await sessions.forEach(async (r: Chat.Session) => {
this.history.unshift(r)
})
this.history = await getChatSessionsByUser()

if (this.history.length === 0) {
const new_chat_text = t('chat.new')
this.addChatSession(await getChatSessionDefault(new_chat_text))
await this.addChatSession(await getChatSessionDefault(new_chat_text))
}

let active_session_uuid = this.history[0].uuid
Expand All @@ -90,8 +86,8 @@ export const useChatStore = defineStore('chat-store', {
}
},

addChatSession(history: Chat.Session, chatData: Chat.Message[] = []) {
createChatSession(history.uuid, history.title, history.model)
async addChatSession(history: Chat.Session, chatData: Chat.Message[] = []) {
await createChatSession(history.uuid, history.title, history.model)
this.history.unshift(history)
this.chat[history.uuid] = chatData
this.active = history.uuid
Expand All @@ -102,15 +98,13 @@ export const useChatStore = defineStore('chat-store', {
const index = this.history.findIndex(item => item.uuid === uuid)
if (index !== -1) {
this.history[index] = { ...this.history[index], ...edit }
// update chat session
await fetchUpdateChatByUuid(uuid, this.history[index])
}
},

async updateChatSessionIfEdited(uuid: string, edit: Partial<Chat.Session>) {
const index = this.history.findIndex(item => item.uuid === uuid)
if (index !== -1) {
// set inactive session to edit mode = false
if (this.history[index].isEdit) {
this.history[index] = { ...this.history[index], ...edit }
await fetchUpdateChatByUuid(uuid, this.history[index])
Expand Down Expand Up @@ -165,7 +159,6 @@ export const useChatStore = defineStore('chat-store', {
return this.chat[keys[0]][index]
return null
}
// const chatIndex = this.chat.findIndex(item => item.uuid === uuid)
if (keys.includes(uuid))
return this.chat[uuid][index]
return null
Expand All @@ -178,15 +171,12 @@ export const useChatStore = defineStore('chat-store', {
if (this.history.length === 0) {
const default_model_parameters = await getChatSessionDefault(new_chat_text)
const uuid = default_model_parameters.uuid;
createChatSession(uuid, chat.text, default_model_parameters.model)
await createChatSession(uuid, chat.text, default_model_parameters.model)
this.history.push({ uuid, title: chat.text, isEdit: false })
// first chat message is prompt
// this.chat.push({ uuid, data: [{ ...chat, isPrompt: true, isPin: false }] })
this.chat[uuid] = [{ ...chat, isPrompt: true, isPin: false }]
this.active = uuid
}
else {
// this.chat[0].data.push(chat)
this.chat[keys[0]].push(chat)
if (this.history[0].title === new_chat_text) {
this.history[0].title = chat.text
Expand All @@ -195,7 +185,6 @@ export const useChatStore = defineStore('chat-store', {
}
}

// const index = this.chat.findIndex(item => item.uuid === uuid)
if (keys.includes(uuid)) {
if (this.chat[uuid].length === 0)
this.chat[uuid].push({ ...chat, isPrompt: true, isPin: false })
Expand All @@ -218,7 +207,6 @@ export const useChatStore = defineStore('chat-store', {
return
}

// const chatIndex = this.chat.findIndex(item => item.uuid === uuid)
if (keys.includes(uuid)) {
this.chat[uuid][index] = chat
}
Expand All @@ -237,7 +225,6 @@ export const useChatStore = defineStore('chat-store', {
return
}

// const chatIndex = this.chat.findIndex(item => item.uuid === uuid)
if (keys.includes(uuid)) {
this.chat[uuid][index] = {
...this.chat[uuid][index],
Expand All @@ -259,7 +246,6 @@ export const useChatStore = defineStore('chat-store', {
return
}

// const chatIndex = this.chat.findIndex(item => item.uuid === uuid)
if (keys.includes(uuid)) {
const chatData = this.chat[uuid]
const chat = chatData[index]
Expand All @@ -270,7 +256,6 @@ export const useChatStore = defineStore('chat-store', {
},

clearChatByUuid(uuid: string) {
// does this every happen?
const [keys, keys_length] = getChatKeys(this.chat)
if (!uuid) {
if (keys_length) {
Expand Down
2 changes: 1 addition & 1 deletion web/src/views/chat/layout/sider/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async function handleAdd() {
// "debug": false
// }//
const default_model_parameters = await getChatSessionDefault(new_chat_text)
chatStore.addChatSession(default_model_parameters)
await chatStore.addChatSession(default_model_parameters)
if (isMobile.value)
appStore.setSiderCollapsed(true)
}
Expand Down

0 comments on commit f7b443c

Please sign in to comment.