refactor chat
This commit is contained in:
1 parent
565b367c59
commit
cd5dbaa264
20 files changed
+192
-230
No files matched your search
@@ -1,9 +1,77 @@
|
||||
import { IApplication } from '@/operators';
|
||||
import {
|
||||
CHAT_MODEL_CHATGPT,
|
||||
CHAT_MODEL_CHATGPT4,
|
||||
CHAT_MODEL_CHATGPT4_BROWSING,
|
||||
CHAT_MODEL_CHATGPT_16K,
|
||||
CHAT_MODEL_CHATGPT_BROWSING,
|
||||
IApplication,
|
||||
IChatConversation,
|
||||
apiUsageOperator,
|
||||
applicationOperator,
|
||||
chatOperator
|
||||
} from '@/operators';
|
||||
import { IRootState, Status } from '../common/models';
|
||||
import { ActionContext } from 'vuex';
|
||||
import { log } from '@/utils/log';
|
||||
import { IChatState } from './models';
|
||||
|
||||
export const setApplications = async ({ commit }: any, payload: IApplication[]) => {
|
||||
export const setApplications = async ({ commit }: any, payload: IApplication[]): Promise<void> => {
|
||||
commit('setApplications', payload);
|
||||
};
|
||||
|
||||
export const getApplications = async ({
|
||||
commit,
|
||||
rootState
|
||||
}: ActionContext<IChatState, IRootState>): Promise<IApplication[]> => {
|
||||
log(getApplications, 'start to get application for chat');
|
||||
commit('getApplicationsStatus', Status.Request);
|
||||
const { data: applications } = await applicationOperator.getAll({
|
||||
user_id: rootState.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
|
||||
]
|
||||
});
|
||||
log(getApplications, 'get application for chat success', applications);
|
||||
commit('getApplicationsStatus', Status.Success);
|
||||
commit('setApplications', applications?.items);
|
||||
return applications.items;
|
||||
};
|
||||
|
||||
export const getConversations = async ({
|
||||
commit,
|
||||
state,
|
||||
rootState
|
||||
}: ActionContext<IChatState, IRootState>): Promise<IChatConversation[]> => {
|
||||
log(getConversations, 'start to get conversations');
|
||||
commit('setGetConversationStatus', Status.Success);
|
||||
const {
|
||||
data: { items: apiUsages }
|
||||
} = await apiUsageOperator.getAll({
|
||||
user_id: rootState.user.id,
|
||||
// @ts-ignore
|
||||
application_id: state.applications?.map((application) => application.id),
|
||||
offset: 0,
|
||||
limit: 30,
|
||||
ordering: '-created_at'
|
||||
});
|
||||
log(getConversations, 'get api usages success', apiUsages);
|
||||
commit('setGetConversationStatus', 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)];
|
||||
log(getConversations, 'get unique conversation ids', uniqueConversationIds);
|
||||
const conversations = (await chatOperator.getConversations(uniqueConversationIds)).data;
|
||||
log(getConversations, 'get conversations success', conversations);
|
||||
commit('setConversations', conversations);
|
||||
return conversations;
|
||||
};
|
||||
|
||||
export default {
|
||||
setApplications
|
||||
setApplications,
|
||||
getApplications,
|
||||
getConversations
|
||||
};
|
||||
@@ -1,7 +1,9 @@
|
||||
import { IApplication } from '@/operators';
|
||||
import { IApplication, IChatConversation } from '@/operators';
|
||||
import { Status } from '../common/models';
|
||||
|
||||
export interface IChatState {
|
||||
applications: IApplication[] | undefined;
|
||||
applicationsStatus: Status | undefined;
|
||||
getApplicationsStatus: Status | undefined;
|
||||
conversations: IChatConversation[] | undefined;
|
||||
getConversationsStatus: Status | undefined;
|
||||
}
|
||||
@@ -6,11 +6,21 @@ export const setApplications = (state: IChatState, payload: IApplication[]): voi
|
||||
state.applications = payload;
|
||||
};
|
||||
|
||||
export const setApplicationsStatus = (state: IChatState, payload: Status): void => {
|
||||
state.applicationsStatus = payload;
|
||||
export const setGetApplicationsStatus = (state: IChatState, payload: Status): void => {
|
||||
state.getApplicationsStatus = payload;
|
||||
};
|
||||
|
||||
export const setConversations = (state: IChatState, payload: any): void => {
|
||||
state.conversations = payload;
|
||||
};
|
||||
|
||||
export const setGetConversationsStatus = (state: IChatState, payload: Status): void => {
|
||||
state.getConversationsStatus = payload;
|
||||
};
|
||||
|
||||
export default {
|
||||
setApplications,
|
||||
setApplicationsStatus
|
||||
setGetApplicationsStatus,
|
||||
setConversations,
|
||||
setGetConversationsStatus
|
||||
};
|
||||
@@ -3,6 +3,8 @@ import { IChatState } from './models';
|
||||
export default (): IChatState => {
|
||||
return {
|
||||
applications: undefined,
|
||||
applicationsStatus: undefined
|
||||
getApplicationsStatus: undefined,
|
||||
conversations: undefined,
|
||||
getConversationsStatus: undefined
|
||||
};
|
||||
};
|
||||
@@ -1,6 +1,8 @@
|
||||
import { ActionContext } from 'vuex';
|
||||
import { IRootState, IToken } from '../common/models';
|
||||
import { IApplication, IUser } from '@/operators';
|
||||
import { IApplication, IUser, userOperator } from '@/operators';
|
||||
import { log } from '@/utils/log';
|
||||
import { oauthOperator } from '@/operators/auth/operator';
|
||||
|
||||
export const resetToken = ({ commit }: ActionContext<IRootState, IRootState>) => {
|
||||
commit('resetToken');
|
||||
@@ -14,13 +16,33 @@ export const setUser = ({ commit }: ActionContext<IRootState, IRootState>, paylo
|
||||
commit('setUser', payload);
|
||||
};
|
||||
|
||||
export const setApplications = ({ commit }: ActionContext<IRootState, IRootState>, payload: IApplication[]) => {
|
||||
commit('setApplications', payload);
|
||||
export const getUser = async ({ commit }: ActionContext<IRootState, IRootState>): Promise<IUser> => {
|
||||
log(getUser, 'start to get user');
|
||||
const { data: user } = await userOperator.getMe();
|
||||
commit('setUser', user);
|
||||
log(getUser, 'get user success', user);
|
||||
return user;
|
||||
};
|
||||
|
||||
export const getToken = async ({ commit }: ActionContext<IRootState, IRootState>, code: string): Promise<IToken> => {
|
||||
log(getToken, 'start to get token using code', code);
|
||||
const { data } = await oauthOperator.token({
|
||||
code
|
||||
});
|
||||
const token = {
|
||||
access: data.access_token,
|
||||
refresh: data.refresh_token,
|
||||
expiration: data.expires_in
|
||||
};
|
||||
commit('setToken', token);
|
||||
log(getToken, 'get token success', data);
|
||||
return token;
|
||||
};
|
||||
|
||||
export default {
|
||||
resetToken,
|
||||
setToken,
|
||||
setUser,
|
||||
setApplications
|
||||
getToken,
|
||||
getUser
|
||||
};
|
||||
@@ -16,14 +16,9 @@ export const setting = (state: IRootState): any => {
|
||||
return state.setting;
|
||||
};
|
||||
|
||||
export const applications = (state: IRootState): any => {
|
||||
return state.applications;
|
||||
};
|
||||
|
||||
export default {
|
||||
authenticated,
|
||||
user,
|
||||
token,
|
||||
setting,
|
||||
applications
|
||||
setting
|
||||
};
|
||||
@@ -11,6 +11,7 @@ export enum Status {
|
||||
export interface IToken {
|
||||
access?: string;
|
||||
refresh?: string;
|
||||
expiration?: number;
|
||||
}
|
||||
|
||||
export interface ISetting {
|
||||
@@ -22,7 +23,6 @@ export interface ICommonState {
|
||||
token: IToken;
|
||||
user: IUser;
|
||||
setting: ISetting;
|
||||
applications: IApplication[] | undefined;
|
||||
}
|
||||
|
||||
export interface IRootState extends ICommonState {
|
||||
|
||||
@@ -26,14 +26,9 @@ export const setSetting = (state: IRootState, payload: any): void => {
|
||||
};
|
||||
};
|
||||
|
||||
export const setApplications = (state: IRootState, payload: any): void => {
|
||||
state.applications = payload;
|
||||
};
|
||||
|
||||
export default {
|
||||
setUser,
|
||||
setToken,
|
||||
resetToken,
|
||||
setSetting,
|
||||
setApplications
|
||||
setSetting
|
||||
};
|
||||
@@ -6,12 +6,12 @@ export default (): IRootState => {
|
||||
user: {},
|
||||
token: {
|
||||
access: undefined,
|
||||
refresh: undefined
|
||||
refresh: undefined,
|
||||
expiration: undefined
|
||||
},
|
||||
setting: {
|
||||
endpoint: ENDPOINT,
|
||||
stream: false
|
||||
},
|
||||
applications: undefined
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user