From 9c69e9641609baaba928ea8bdb8db7409f1d3f30 Mon Sep 17 00:00:00 2001 From: AceDataCloud Date: Sat, 30 Dec 2023 12:17:13 +0800 Subject: [PATCH] update vuex --- src/layouts/Chat.vue | 55 ++++++++++++++++++++++++++++++++++++++++++++ src/store/chat.ts | 10 +++++--- src/store/models.ts | 7 ++++++ 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/layouts/Chat.vue b/src/layouts/Chat.vue index 7f80041c..73d1e358 100644 --- a/src/layouts/Chat.vue +++ b/src/layouts/Chat.vue @@ -14,12 +14,67 @@ import { defineComponent } from 'vue'; import Navigator from '@/components/common/Navigator.vue'; import Sidebar from '@/components/chat/Sidebar.vue'; +import { + CHAT_MODEL_CHATGPT, + CHAT_MODEL_CHATGPT4, + CHAT_MODEL_CHATGPT4_BROWSING, + CHAT_MODEL_CHATGPT_16K, + CHAT_MODEL_CHATGPT_BROWSING, + applicationOperator +} from '@/operators'; export default defineComponent({ name: 'LayoutChat', components: { Sidebar, Navigator + }, + data() { + return { + initializing: false, + applications: this.$store.state.chat.applications || undefined + }; + }, + mounted() { + this.onFetchApplications(); + }, + methods: { + 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; + }, + 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.$store.dispatch('chat/setApplications', applications?.items); + } } }); diff --git a/src/store/chat.ts b/src/store/chat.ts index f462c654..bab414ee 100644 --- a/src/store/chat.ts +++ b/src/store/chat.ts @@ -1,21 +1,25 @@ import { IApplication } from '@/operators'; import { Module } from 'vuex'; -import { IChatState } from './models'; +import { IChatState, Status } from './models'; export const chat: Module = { namespaced: true, state(): IChatState { return { - applications: undefined + applications: undefined, + applicationsStatus: undefined }; }, mutations: { setApplications(state: IChatState, payload: IApplication[]): void { state.applications = payload; + }, + setApplicationsStatus(state: IChatState, payload: Status): void { + state.applicationsStatus = payload; } }, actions: { - setApplications({ commit }: any, payload: IApplication[]) { + async setApplications({ commit }: any, payload: IApplication[]) { commit('setApplications', payload); } } diff --git a/src/store/models.ts b/src/store/models.ts index d52f7c3c..0d970476 100644 --- a/src/store/models.ts +++ b/src/store/models.ts @@ -1,5 +1,11 @@ import { IApplication, IMidjourneyPreset, IUser } from '@/operators'; +export enum Status { + Request = 'Request', + Success = 'Success', + Error = 'Error' +} + export interface IToken { access?: string; refresh?: string; @@ -24,6 +30,7 @@ export interface IMidjourneyState { export interface IChatState { applications: IApplication[] | undefined; + applicationsStatus: Status | undefined; } export interface IRootState {