finish refine vuex for chat
This commit is contained in:
1 parent
cd5dbaa264
commit
6ab318e3f0
9 files changed
+73
-56
No files matched your search
@@ -1,7 +1,7 @@
|
||||
<template>
|
||||
<div class="sidebar">
|
||||
<el-skeleton v-if="loading && conversations === undefined" />
|
||||
<div v-else-if="conversations?.length === 0" class="conversations">
|
||||
<div v-else class="conversations">
|
||||
<div class="conversation" @click="onNewConversation">
|
||||
<div class="icons">
|
||||
<font-awesome-icon icon="fa-solid fa-plus" class="icon" />
|
||||
@@ -10,8 +10,6 @@
|
||||
{{ $t('chat.message.startNewChat') }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div v-else class="conversations">
|
||||
<div
|
||||
v-for="(conversation, conversationIndex) in conversations"
|
||||
:key="conversationIndex"
|
||||
@@ -71,9 +69,10 @@ 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 { IApplication, chatOperator } from '@/operators';
|
||||
import { chatOperator } from '@/operators';
|
||||
import { IChatConversation } from '@/operators/chat/models';
|
||||
import { Status } from '@/store/common/models';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
|
||||
export default defineComponent({
|
||||
name: 'SidePanel',
|
||||
@@ -95,20 +94,13 @@ export default defineComponent({
|
||||
return this.$store.state.chat.getConversationsStatus === Status.Request;
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
applications: {
|
||||
async handler(val: IApplication[]) {
|
||||
if (!val) {
|
||||
return;
|
||||
}
|
||||
await this.$store.dispatch('chat/getConversations');
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
async onNewConversation() {
|
||||
this.$router.push({
|
||||
name: ROUTE_CHAT_CONVERSATION_NEW
|
||||
name: ROUTE_CHAT_CONVERSATION,
|
||||
params: {
|
||||
id: uuid()
|
||||
}
|
||||
});
|
||||
},
|
||||
async onConfirm(conversation: IChatConversation) {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<el-button
|
||||
:class="{
|
||||
button: true,
|
||||
active: $route.name === link.route.name
|
||||
active: link.routes.includes($route.name as string)
|
||||
}"
|
||||
@click="$router.push(link.route)"
|
||||
>
|
||||
@@ -28,7 +28,13 @@
|
||||
import { defineComponent } from 'vue';
|
||||
import { ElButton } from 'element-plus';
|
||||
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
|
||||
import { ROUTE_AUTH_LOGIN, ROUTE_CHAT_INDEX, ROUTE_MIDJOURNEY_INDEX } from '@/router/constants';
|
||||
import {
|
||||
ROUTE_AUTH_LOGIN,
|
||||
ROUTE_CHAT_CONVERSATION,
|
||||
ROUTE_CHAT_INDEX,
|
||||
ROUTE_MIDJOURNEY_HISTORY,
|
||||
ROUTE_MIDJOURNEY_INDEX
|
||||
} from '@/router/constants';
|
||||
|
||||
export default defineComponent({
|
||||
name: 'Navigator',
|
||||
@@ -43,13 +49,15 @@ export default defineComponent({
|
||||
route: {
|
||||
name: ROUTE_CHAT_INDEX
|
||||
},
|
||||
icon: 'fa-regular fa-comment'
|
||||
icon: 'fa-regular fa-comment',
|
||||
routes: [ROUTE_CHAT_INDEX, ROUTE_CHAT_CONVERSATION]
|
||||
},
|
||||
{
|
||||
route: {
|
||||
name: ROUTE_MIDJOURNEY_INDEX
|
||||
},
|
||||
icon: 'fa-solid fa-palette'
|
||||
icon: 'fa-solid fa-palette',
|
||||
routes: [ROUTE_MIDJOURNEY_INDEX, ROUTE_MIDJOURNEY_HISTORY]
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
@@ -47,6 +47,7 @@ export interface IChatConversation {
|
||||
title?: string;
|
||||
deleting?: boolean;
|
||||
editing?: boolean;
|
||||
new?: boolean;
|
||||
}
|
||||
|
||||
export interface IChatAskOptions {
|
||||
|
||||
@@ -27,26 +27,25 @@ import {
|
||||
ROLE_ASSISTANT,
|
||||
ROLE_USER,
|
||||
IChatModel,
|
||||
IChatMessage,
|
||||
CHAT_MODEL_CHATGPT,
|
||||
applicationOperator,
|
||||
IApplication,
|
||||
IChatMessageState,
|
||||
IChatAskResponse,
|
||||
chatOperator
|
||||
chatOperator,
|
||||
IChatConversation
|
||||
} from '@/operators';
|
||||
import InputBox from '@/components/chat/InputBox.vue';
|
||||
import ModelSelector from '@/components/chat/ModelSelector.vue';
|
||||
import { ERROR_CODE_CANCELED, ERROR_CODE_UNKNOWN } from '@/constants/errorCode';
|
||||
import ApiStatus from '@/components/common/ApiStatus.vue';
|
||||
import { ROUTE_CHAT_CONVERSATION, ROUTE_CHAT_CONVERSATION_NEW } from '@/router';
|
||||
import { Status } from '@/store/common/models';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
|
||||
export interface IData {
|
||||
messages: IChatMessage[];
|
||||
model: IChatModel;
|
||||
question: '';
|
||||
initializing: boolean;
|
||||
applied: boolean | undefined;
|
||||
// messages: IChatMessage[];
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
@@ -60,13 +59,17 @@ export default defineComponent({
|
||||
data(): IData {
|
||||
return {
|
||||
model: CHAT_MODEL_CHATGPT,
|
||||
question: '',
|
||||
initializing: false,
|
||||
applied: undefined,
|
||||
messages: []
|
||||
question: ''
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
messages() {
|
||||
return (
|
||||
this.$store.state.chat.conversations?.find(
|
||||
(conversation: IChatConversation) => conversation.id === this.$route.params.id?.toString()
|
||||
)?.messages || []
|
||||
);
|
||||
},
|
||||
conversationId(): string | undefined {
|
||||
return this.$route.params.id?.toString();
|
||||
},
|
||||
@@ -75,24 +78,33 @@ export default defineComponent({
|
||||
},
|
||||
applications() {
|
||||
return this.$store.state.chat.applications;
|
||||
},
|
||||
conversations() {
|
||||
return this.$store.state.chat.conversations;
|
||||
},
|
||||
initializing() {
|
||||
return this.$store.state.chat.getApplicationsStatus === Status.Request;
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
conversationId(val) {
|
||||
if (val) {
|
||||
this.onFetchHistory(val);
|
||||
}
|
||||
}
|
||||
},
|
||||
async mounted() {
|
||||
await this.onFetchHistory();
|
||||
},
|
||||
methods: {
|
||||
async onCreateNewConversation() {
|
||||
this.messages = [];
|
||||
const newConversationId = uuid();
|
||||
await this.$router.push({
|
||||
name: ROUTE_CHAT_CONVERSATION_NEW
|
||||
name: ROUTE_CHAT_CONVERSATION,
|
||||
params: {
|
||||
id: newConversationId
|
||||
}
|
||||
});
|
||||
const conversations = this.conversations;
|
||||
console.log('this.store', this.$store);
|
||||
await this.$store.dispatch('chat/setConversations', [
|
||||
{
|
||||
id: newConversationId,
|
||||
messages: [],
|
||||
new: true
|
||||
},
|
||||
...conversations
|
||||
]);
|
||||
},
|
||||
async onSelectModel() {
|
||||
await this.onCreateNewConversation();
|
||||
|
||||
@@ -11,7 +11,8 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent } from 'vue';
|
||||
import { ElButton } from 'element-plus';
|
||||
import { ROUTE_CHAT_CONVERSATION_NEW } from '@/router/constants';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import { ROUTE_CHAT_CONVERSATION } from '@/router/constants';
|
||||
|
||||
interface IData {
|
||||
model: string;
|
||||
@@ -32,7 +33,10 @@ export default defineComponent({
|
||||
methods: {
|
||||
onStart() {
|
||||
this.$router.push({
|
||||
name: ROUTE_CHAT_CONVERSATION_NEW
|
||||
name: ROUTE_CHAT_CONVERSATION,
|
||||
params: {
|
||||
id: uuid()
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+1
-6
@@ -1,4 +1,4 @@
|
||||
import { ROUTE_CHAT_CONVERSATION, ROUTE_CHAT_CONVERSATION_NEW, ROUTE_CHAT_INDEX } from './constants';
|
||||
import { ROUTE_CHAT_CONVERSATION, ROUTE_CHAT_INDEX } from './constants';
|
||||
|
||||
export default {
|
||||
path: '/chat',
|
||||
@@ -9,11 +9,6 @@ export default {
|
||||
name: ROUTE_CHAT_INDEX,
|
||||
component: () => import('@/pages/chat/Index.vue')
|
||||
},
|
||||
{
|
||||
path: 'conversation',
|
||||
name: ROUTE_CHAT_CONVERSATION_NEW,
|
||||
component: () => import('@/pages/chat/Conversation.vue')
|
||||
},
|
||||
{
|
||||
path: 'conversation/:id',
|
||||
name: ROUTE_CHAT_CONVERSATION,
|
||||
|
||||
@@ -5,7 +5,6 @@ export const ROUTE_AUTH_CALLBACK = 'auth-callback';
|
||||
|
||||
export const ROUTE_CHAT_INDEX = 'chat-index';
|
||||
export const ROUTE_CHAT_CONVERSATION = 'chat-conversation';
|
||||
export const ROUTE_CHAT_CONVERSATION_NEW = 'chat-conversation-new';
|
||||
|
||||
export const ROUTE_MIDJOURNEY_INDEX = 'midjourney-index';
|
||||
export const ROUTE_MIDJOURNEY_HISTORY = 'midjourney-history';
|
||||
@@ -19,12 +19,17 @@ export const setApplications = async ({ commit }: any, payload: IApplication[]):
|
||||
commit('setApplications', payload);
|
||||
};
|
||||
|
||||
export const setConversations = async ({ commit }: any, payload: IChatConversation[]): Promise<void> => {
|
||||
log(setConversations, 'set conversations', payload);
|
||||
commit('setConversations', payload);
|
||||
};
|
||||
|
||||
export const getApplications = async ({
|
||||
commit,
|
||||
rootState
|
||||
}: ActionContext<IChatState, IRootState>): Promise<IApplication[]> => {
|
||||
log(getApplications, 'start to get application for chat');
|
||||
commit('getApplicationsStatus', Status.Request);
|
||||
commit('setGetApplicationsStatus', Status.Request);
|
||||
const { data: applications } = await applicationOperator.getAll({
|
||||
user_id: rootState.user.id,
|
||||
api_id: [
|
||||
@@ -36,7 +41,7 @@ export const getApplications = async ({
|
||||
]
|
||||
});
|
||||
log(getApplications, 'get application for chat success', applications);
|
||||
commit('getApplicationsStatus', Status.Success);
|
||||
commit('setGetApplicationsStatus', Status.Success);
|
||||
commit('setApplications', applications?.items);
|
||||
return applications.items;
|
||||
};
|
||||
@@ -47,7 +52,7 @@ export const getConversations = async ({
|
||||
rootState
|
||||
}: ActionContext<IChatState, IRootState>): Promise<IChatConversation[]> => {
|
||||
log(getConversations, 'start to get conversations');
|
||||
commit('setGetConversationStatus', Status.Success);
|
||||
commit('setGetConversationsStatus', Status.Success);
|
||||
const {
|
||||
data: { items: apiUsages }
|
||||
} = await apiUsageOperator.getAll({
|
||||
@@ -59,7 +64,7 @@ export const getConversations = async ({
|
||||
ordering: '-created_at'
|
||||
});
|
||||
log(getConversations, 'get api usages success', apiUsages);
|
||||
commit('setGetConversationStatus', Status.Success);
|
||||
commit('setGetConversationsStatus', Status.Success);
|
||||
// de duplicate conversations using id
|
||||
const conversationIds: string[] = apiUsages.map((apiUsage) => apiUsage.metadata?.conversation_id).filter((id) => id);
|
||||
const uniqueConversationIds = [...new Set(conversationIds)];
|
||||
@@ -72,6 +77,7 @@ export const getConversations = async ({
|
||||
|
||||
export default {
|
||||
setApplications,
|
||||
setConversations,
|
||||
getApplications,
|
||||
getConversations
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IApplication } from '@/operators';
|
||||
import { IApplication, IChatConversation } from '@/operators';
|
||||
import { IChatState } from './models';
|
||||
import { Status } from '../common/models';
|
||||
|
||||
@@ -10,7 +10,7 @@ export const setGetApplicationsStatus = (state: IChatState, payload: Status): vo
|
||||
state.getApplicationsStatus = payload;
|
||||
};
|
||||
|
||||
export const setConversations = (state: IChatState, payload: any): void => {
|
||||
export const setConversations = (state: IChatState, payload: IChatConversation[]): void => {
|
||||
state.conversations = payload;
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user