refactor chat
This commit is contained in:
1 parent
565b367c59
commit
cd5dbaa264
20 files changed
+192
-230
No files matched your search
+17
-33
@@ -1,14 +1,15 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent } from 'vue';
|
||||
import { getAuthBaseUrl, getHubBaseUrl } from '@/utils';
|
||||
import { oauthOperator } from '@/operators/auth/operator';
|
||||
import { applicationOperator, userOperator } from '@/operators';
|
||||
import { IUser } from '@/operators';
|
||||
import { IToken } from '@/operators/auth/models';
|
||||
import { ROUTE_AUTH_LOGIN } from '@/router';
|
||||
|
||||
interface IData {
|
||||
accessToken: string | undefined;
|
||||
refreshToken: string | undefined;
|
||||
redirect: string | undefined;
|
||||
code: string | undefined;
|
||||
user: IUser | undefined;
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
@@ -18,46 +19,29 @@ export default defineComponent({
|
||||
accessToken: undefined,
|
||||
refreshToken: undefined,
|
||||
redirect: this.$route.query.redirect?.toString(),
|
||||
code: this.$route.query.code?.toString()
|
||||
code: this.$route.query.code?.toString(),
|
||||
user: undefined
|
||||
};
|
||||
},
|
||||
async mounted() {
|
||||
// if auth code is provided, get token via oauth
|
||||
if (this.code) {
|
||||
console.debug('code found, try to get tokens');
|
||||
const { data } = await oauthOperator.token({
|
||||
code: this.code
|
||||
});
|
||||
this.accessToken = data.access_token;
|
||||
this.refreshToken = data.refresh_token;
|
||||
console.debug('get tokens successfully');
|
||||
const data: IToken = await this.$store.dispatch('getToken', this.code);
|
||||
this.accessToken = data.access;
|
||||
this.refreshToken = data.refresh;
|
||||
} else {
|
||||
this.$router.push({ name: ROUTE_AUTH_LOGIN });
|
||||
return;
|
||||
}
|
||||
// if token acquired, get user info
|
||||
if (this.accessToken && this.refreshToken) {
|
||||
// store token to global store
|
||||
await this.$store.dispatch('common/setToken', {
|
||||
refresh: this.refreshToken,
|
||||
access: this.accessToken
|
||||
});
|
||||
console.debug('set token successfully');
|
||||
const { data: user } = await userOperator.getMe();
|
||||
await this.$store.dispatch('common/setUser', user);
|
||||
console.debug('set user successfully');
|
||||
const { data: applications } = await applicationOperator.getAll({
|
||||
user_id: user.id
|
||||
});
|
||||
await this.$store.dispatch('common/setApplications', applications);
|
||||
console.debug('set applications successfully');
|
||||
const user = await this.$store.dispatch('getUser');
|
||||
this.user = user;
|
||||
if (this.redirect) {
|
||||
this.$router.push(this.redirect);
|
||||
}
|
||||
} else {
|
||||
console.debug('access token and refresh token not found, try to re-auth again');
|
||||
const hubBaseUrl = getHubBaseUrl();
|
||||
const authBaseUrl = getAuthBaseUrl();
|
||||
// callback url used to init access token and then redirect back of `redirect`
|
||||
const callbackUrl = `${hubBaseUrl}/auth/callback?redirect=${this.redirect}`;
|
||||
// redirect to auth service to get access token then redirect back
|
||||
const targetUrl = `${authBaseUrl}/auth/login?redirect=${callbackUrl}`;
|
||||
window.location.href = targetUrl;
|
||||
this.$router.push({ name: ROUTE_AUTH_LOGIN });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
:initializing="initializing"
|
||||
:application="application"
|
||||
:api-id="model.apiId"
|
||||
@refresh="onFetchApplications"
|
||||
@refresh="$store.dispatch('chat/getApplications')"
|
||||
/>
|
||||
<div class="dialogue">
|
||||
<introduction v-if="messages && messages.length === 0" />
|
||||
@@ -33,10 +33,6 @@ import {
|
||||
IApplication,
|
||||
IChatMessageState,
|
||||
IChatAskResponse,
|
||||
CHAT_MODEL_CHATGPT_16K,
|
||||
CHAT_MODEL_CHATGPT_BROWSING,
|
||||
CHAT_MODEL_CHATGPT4,
|
||||
CHAT_MODEL_CHATGPT4_BROWSING,
|
||||
chatOperator
|
||||
} from '@/operators';
|
||||
import InputBox from '@/components/chat/InputBox.vue';
|
||||
@@ -48,7 +44,6 @@ import { ROUTE_CHAT_CONVERSATION, ROUTE_CHAT_CONVERSATION_NEW } from '@/router';
|
||||
export interface IData {
|
||||
messages: IChatMessage[];
|
||||
model: IChatModel;
|
||||
applications: IApplication[] | undefined;
|
||||
question: '';
|
||||
initializing: boolean;
|
||||
applied: boolean | undefined;
|
||||
@@ -68,7 +63,6 @@ export default defineComponent({
|
||||
question: '',
|
||||
initializing: false,
|
||||
applied: undefined,
|
||||
applications: undefined,
|
||||
messages: []
|
||||
};
|
||||
},
|
||||
@@ -77,7 +71,10 @@ export default defineComponent({
|
||||
return this.$route.params.id?.toString();
|
||||
},
|
||||
application() {
|
||||
return this.applications?.find((application) => application.api?.id === this.model.apiId);
|
||||
return this.applications?.find((application: IApplication) => application.api?.id === this.model.apiId);
|
||||
},
|
||||
applications() {
|
||||
return this.$store.state.chat.applications;
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
@@ -88,7 +85,6 @@ export default defineComponent({
|
||||
}
|
||||
},
|
||||
async mounted() {
|
||||
await this.onFetchApplications();
|
||||
await this.onFetchHistory();
|
||||
},
|
||||
methods: {
|
||||
@@ -100,22 +96,7 @@ export default defineComponent({
|
||||
},
|
||||
async onSelectModel() {
|
||||
await this.onCreateNewConversation();
|
||||
await this.onFetchApplications();
|
||||
},
|
||||
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;
|
||||
await this.$store.dispatch('chat/getApplications');
|
||||
},
|
||||
async onSubmitQuestion() {
|
||||
this.messages.push({
|
||||
@@ -174,7 +155,7 @@ export default defineComponent({
|
||||
)
|
||||
.then(() => {
|
||||
this.messages[this.messages.length - 1].state = IChatMessageState.FINISHED;
|
||||
this.onFetchApplications();
|
||||
this.$store.dispatch('chat/getConversations');
|
||||
})
|
||||
.catch((error) => {
|
||||
if (this.messages && this.messages.length > 0) {
|
||||
|
||||
@@ -103,7 +103,7 @@ export default defineComponent({
|
||||
async onFetchApplications() {
|
||||
this.initializing = true;
|
||||
const { data: applications } = await applicationOperator.getAll({
|
||||
user_id: this.$store.state.common.user.id,
|
||||
user_id: this.$store.state.user.id,
|
||||
api_id: [MIDJOURNEY_CHANNEL_FAST.apiId, MIDJOURNEY_CHANNEL_RELAX.apiId, MIDJOURNEY_CHANNEL_TURBO.apiId]
|
||||
});
|
||||
this.initializing = false;
|
||||
|
||||
@@ -177,7 +177,7 @@ export default defineComponent({
|
||||
async onFetchApplications() {
|
||||
this.initializing = true;
|
||||
const { data: applications } = await applicationOperator.getAll({
|
||||
user_id: this.$store.state.common.user.id,
|
||||
user_id: this.$store.state.user.id,
|
||||
api_id: [MIDJOURNEY_CHANNEL_FAST.apiId, MIDJOURNEY_CHANNEL_RELAX.apiId, MIDJOURNEY_CHANNEL_TURBO.apiId]
|
||||
});
|
||||
this.initializing = false;
|
||||
|
||||
Reference in new issue
Block a user