From b1cc9737d189d6d7de3eca545d8e8ed41e69531d Mon Sep 17 00:00:00 2001 From: Germey Date: Sat, 30 Dec 2023 15:23:03 +0800 Subject: [PATCH] refactor chat --- .../chat/{Sidebar.vue => SidePanel.vue} | 96 ++++++------------- src/components/midjourney/ReferenceImage.vue | 2 +- .../midjourney/tasks/TaskBriefList.vue | 2 +- .../midjourney/tasks/TaskFullList.vue | 2 +- src/layouts/Chat.vue | 71 ++------------ src/operators/instance.ts | 3 +- src/pages/auth/Callback.vue | 50 ++++------ src/pages/chat/Conversation.vue | 33 ++----- src/pages/midjourney/History.vue | 2 +- src/pages/midjourney/Index.vue | 2 +- src/store/chat/actions.ts | 74 +++++++++++++- src/store/chat/models.ts | 6 +- src/store/chat/mutations.ts | 16 +++- src/store/chat/state.ts | 4 +- src/store/common/actions.ts | 30 +++++- src/store/common/getters.ts | 7 +- src/store/common/models.ts | 2 +- src/store/common/mutations.ts | 7 +- src/store/common/state.ts | 6 +- src/utils/log.ts | 3 + 20 files changed, 190 insertions(+), 228 deletions(-) rename src/components/chat/{Sidebar.vue => SidePanel.vue} (68%) create mode 100644 src/utils/log.ts diff --git a/src/components/chat/Sidebar.vue b/src/components/chat/SidePanel.vue similarity index 68% rename from src/components/chat/Sidebar.vue rename to src/components/chat/SidePanel.vue index 2060b08..ceb7540 100644 --- a/src/components/chat/Sidebar.vue +++ b/src/components/chat/SidePanel.vue @@ -71,25 +71,12 @@ import { defineComponent } from 'vue'; import { ElSkeleton, ElInput } from 'element-plus'; import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'; import { ROUTE_CHAT_CONVERSATION, ROUTE_CHAT_CONVERSATION_NEW } from '@/router/constants'; -import { apiUsageOperator, IApplication, chatOperator, applicationOperator } from '@/operators'; +import { IApplication, chatOperator } from '@/operators'; import { IChatConversation } from '@/operators/chat/models'; -import { - CHAT_MODEL_CHATGPT, - CHAT_MODEL_CHATGPT4, - CHAT_MODEL_CHATGPT4_BROWSING, - CHAT_MODEL_CHATGPT_16K, - CHAT_MODEL_CHATGPT_BROWSING -} from '@/operators'; - -interface IData { - initializing: boolean; - loading: boolean; - conversations: IChatConversation[] | undefined; - applications: IApplication[] | undefined; -} +import { Status } from '@/store/common/models'; export default defineComponent({ - name: 'Sidebar', + name: 'SidePanel', components: { ElInput, FontAwesomeIcon, @@ -97,78 +84,44 @@ export default defineComponent({ }, props: {}, emits: ['click'], - data(): IData { - return { - initializing: false, - loading: false, - conversations: undefined, - applications: undefined - }; + computed: { + conversations() { + return this.$store.state.chat.conversations; + }, + applications() { + return this.$store.state.chat.applications; + }, + loading() { + return this.$store.state.chat.getConversationsStatus === Status.Request; + } }, watch: { - applications(val) { - if (val) { - this.onFetchConversations(); + applications: { + async handler(val: IApplication[]) { + if (!val) { + return; + } + await this.$store.dispatch('chat/getConversations'); } } }, - async mounted() { - await this.onFetchApplications(); - await this.onFetchConversations(); - }, methods: { async onNewConversation() { this.$router.push({ name: ROUTE_CHAT_CONVERSATION_NEW }); }, - async onFetchApplications() { - this.initializing = true; - const { data: applications } = await applicationOperator.getAll({ - user_id: this.$store.state.common.user.id, - api_id: [ - CHAT_MODEL_CHATGPT.apiId, - CHAT_MODEL_CHATGPT_16K.apiId, - CHAT_MODEL_CHATGPT_BROWSING.apiId, - CHAT_MODEL_CHATGPT4.apiId, - CHAT_MODEL_CHATGPT4_BROWSING.apiId - ] - }); - this.initializing = false; - this.applications = applications?.items; - }, async onConfirm(conversation: IChatConversation) { if (conversation?.deleting) { await chatOperator.deleteConversation(conversation.id); - await this.onFetchConversations(); + await this.$store.dispatch('chat/getConversations'); } else if (conversation?.editing) { await chatOperator.updateConversation(conversation); - this.onFetchConversations(); + await this.$store.dispatch('chat/getConversations'); } else { conversation.editing = true; } }, - async onFetchConversations() { - this.loading = true; - const { - data: { items: apiUsages } - } = await apiUsageOperator.getAll({ - user_id: this.$store.state.common.user.id, - // @ts-ignore - application_id: this.applications?.map((application) => application.id), - offset: 0, - limit: 30, - ordering: '-created_at' - }); - this.loading = false; - // de duplicate conversations using id - const conversationIds: string[] = apiUsages - .map((apiUsage) => apiUsage.metadata?.conversation_id) - .filter((id) => id); - const uniqueConversationIds = [...new Set(conversationIds)]; - const conversations = (await chatOperator.getConversations(uniqueConversationIds)).data; - this.conversations = conversations; - }, onClick(id: string) { if (!id) { return; @@ -191,8 +144,13 @@ export default defineComponent({ flex-direction: column; align-items: flex-end; padding: 15px; + width: 300px; + height: 100%; + border-right: 1px solid #eee; + overflow-y: scroll; + .conversations { - flex: 1; + width: 100%; display: flex; flex-direction: column; align-items: center; diff --git a/src/components/midjourney/ReferenceImage.vue b/src/components/midjourney/ReferenceImage.vue index 0df469f..777715d 100644 --- a/src/components/midjourney/ReferenceImage.vue +++ b/src/components/midjourney/ReferenceImage.vue @@ -50,7 +50,7 @@ export default defineComponent({ computed: { headers() { return { - Authorization: `Bearer ${this.$store.state.common.token.access}` + Authorization: `Bearer ${this.$store.state.token.access}` }; }, urls() { diff --git a/src/components/midjourney/tasks/TaskBriefList.vue b/src/components/midjourney/tasks/TaskBriefList.vue index 97849a0..22c8a46 100644 --- a/src/components/midjourney/tasks/TaskBriefList.vue +++ b/src/components/midjourney/tasks/TaskBriefList.vue @@ -79,7 +79,7 @@ export default defineComponent({ const { data: { items: apiUsages } } = await apiUsageOperator.getAll({ - user_id: this.$store.state.common.user.id, + user_id: this.$store.state.user.id, application_id: this.applications?.map((application) => application.id), offset: 0, limit: 5, diff --git a/src/components/midjourney/tasks/TaskFullList.vue b/src/components/midjourney/tasks/TaskFullList.vue index e69a493..2a26f99 100644 --- a/src/components/midjourney/tasks/TaskFullList.vue +++ b/src/components/midjourney/tasks/TaskFullList.vue @@ -116,7 +116,7 @@ export default defineComponent({ const { data: { items: apiUsages, count } } = await apiUsageOperator.getAll({ - user_id: this.$store.state.common.user.id, + user_id: this.$store.state.user.id, application_id: this.applications?.map((application) => application.id), offset: (this.page - 1) * this.limit, limit: this.limit, diff --git a/src/layouts/Chat.vue b/src/layouts/Chat.vue index 73d1e35..be0e9b9 100644 --- a/src/layouts/Chat.vue +++ b/src/layouts/Chat.vue @@ -4,7 +4,7 @@
- +
@@ -13,68 +13,17 @@ @@ -97,14 +46,6 @@ export default defineComponent({ height: 100%; display: flex; flex-direction: row; - - .sidebar { - display: block; - width: 300px; - height: 100%; - border-right: 1px solid #eee; - overflow-y: scroll; - } } } diff --git a/src/operators/instance.ts b/src/operators/instance.ts index 2a85a70..1d23a5c 100644 --- a/src/operators/instance.ts +++ b/src/operators/instance.ts @@ -15,7 +15,8 @@ const httpClient: AxiosInstance = axios.create({ }); httpClient.interceptors.request.use((config) => { - const accessToken = store.state.common.token?.access; + console.log('store', store.state); + const accessToken = store.state.token?.access; if (!config.headers) { config.headers = {}; } diff --git a/src/pages/auth/Callback.vue b/src/pages/auth/Callback.vue index 5ce93b2..1e0efa6 100644 --- a/src/pages/auth/Callback.vue +++ b/src/pages/auth/Callback.vue @@ -1,14 +1,15 @@