From ca5f39b3fa960f28c45f7c1dee9de76c58924abc Mon Sep 17 00:00:00 2001 From: AceDataCloud Date: Wed, 12 Apr 2023 01:21:07 +0800 Subject: [PATCH] fix --- src/components/common/LeftNavigator.vue | 9 +- src/components/common/SettingPanel.vue | 74 +++++++ src/components/conversation/Message.vue | 2 +- src/components/conversation/NewMessageBox.vue | 28 ++- src/constants/endpoint.ts | 2 + src/constants/index.ts | 2 + src/i18n/zh/application/message.ts | 2 +- src/i18n/zh/common/nav.ts | 1 + src/i18n/zh/conversation/message.ts | 2 +- src/i18n/zh/index.ts | 4 +- src/i18n/zh/setting/field.ts | 4 + src/i18n/zh/setting/index.ts | 3 + src/operators/api/chatgpt/operator.ts | 5 +- src/pages/conversation/Main.vue | 192 ++++++++++-------- src/plugins/font-awesome.ts | 2 + src/store/index.ts | 25 ++- 16 files changed, 244 insertions(+), 113 deletions(-) create mode 100644 src/components/common/SettingPanel.vue create mode 100644 src/constants/endpoint.ts create mode 100644 src/i18n/zh/setting/field.ts create mode 100644 src/i18n/zh/setting/index.ts diff --git a/src/components/common/LeftNavigator.vue b/src/components/common/LeftNavigator.vue index e63236eb..5c6832e8 100644 --- a/src/components/common/LeftNavigator.vue +++ b/src/components/common/LeftNavigator.vue @@ -13,6 +13,8 @@ />
+ + + +

{{ $t('common.nav.setting') }}

+ + + + + + + + + + +
+ + + + + diff --git a/src/components/conversation/Message.vue b/src/components/conversation/Message.vue index 3cd46a45..5ddaf8eb 100644 --- a/src/components/conversation/Message.vue +++ b/src/components/conversation/Message.vue @@ -60,7 +60,7 @@ export default defineComponent({ } } }, -emits: ['stop'], + emits: ['stop'], data(): IData { return { copied: false diff --git a/src/components/conversation/NewMessageBox.vue b/src/components/conversation/NewMessageBox.vue index a516e3f1..c34d5860 100644 --- a/src/components/conversation/NewMessageBox.vue +++ b/src/components/conversation/NewMessageBox.vue @@ -4,7 +4,7 @@ v-model="value" type="textarea" resize="none" - :disabled="!application || answering" + :disabled="initializing || !application || answering" class="new-message-input" :placeholder="$t('conversation.message.newMessagePlaceholder')" @keydown.enter.exact.prevent="$emit('send')" @@ -15,19 +15,17 @@
-
-
- {{ $t('conversation.message.apiInfo') }} - - - {{ $t('common.button.apply') }} - - - {{ $t('conversation.message.tryForFree') }} -
-
- {{ $t('conversation.message.initializing') }} -
+
+ {{ $t('conversation.message.initializing') }} +
+
+ {{ $t('conversation.message.apiInfo') }} + + + {{ $t('common.button.apply') }} + + + {{ $t('conversation.message.tryForFree') }}
import { defineComponent } from 'vue'; -import { ElInput, ElButton, ElText } from 'element-plus'; +import { ElInput, ElButton } from 'element-plus'; import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'; import { IApi, IApplication } from '@/operators'; import { IApplicationType } from '@/operators'; diff --git a/src/constants/endpoint.ts b/src/constants/endpoint.ts new file mode 100644 index 00000000..60eb8e54 --- /dev/null +++ b/src/constants/endpoint.ts @@ -0,0 +1,2 @@ +export const ENDPOINT = 'https://api.zhishuyun.com'; +export const ENDPOINT2 = 'https://api2.zhishuyun.com'; diff --git a/src/constants/index.ts b/src/constants/index.ts index 2b3e4aa3..a4768bd1 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -1,2 +1,4 @@ export * from './errorCode'; export * from './env'; +export * from './endpoint'; +export * from './image'; diff --git a/src/i18n/zh/application/message.ts b/src/i18n/zh/application/message.ts index e9e98e50..3579ec7a 100644 --- a/src/i18n/zh/application/message.ts +++ b/src/i18n/zh/application/message.ts @@ -17,5 +17,5 @@ export default { rotateCredentialSuccessfully: '密钥重置成功', rotateCredentialFailed: '密钥重置失败,请联系客服处理', updateFailed: '修改失败', - updateSuccessfully: '修改成功', + updateSuccessfully: '修改成功' }; diff --git a/src/i18n/zh/common/nav.ts b/src/i18n/zh/common/nav.ts index 48d403a0..865294b1 100644 --- a/src/i18n/zh/common/nav.ts +++ b/src/i18n/zh/common/nav.ts @@ -1,5 +1,6 @@ export default { editor: '编辑器', + setting: '设置', home: '首页', content: '内容', platform: '平台', diff --git a/src/i18n/zh/conversation/message.ts b/src/i18n/zh/conversation/message.ts index dfab6905..46f6bed0 100644 --- a/src/i18n/zh/conversation/message.ts +++ b/src/i18n/zh/conversation/message.ts @@ -5,7 +5,7 @@ export default { apiInfo: '本服务由「知数云」提供技术支持', tryForFree: '免费使用', initializing: '初始化中,请稍后...', - writeCode: '写一段 JavaScript 代码,发送HTTP请求', + writeCode: '写一段 JavaScript 代码,输出 Hello World', writeEmail: '写一封邮件给 Bob,告诉他我刚刚搬家了', explainDefinition: '用简单的术语解释量子计算', rememberHistory: '可以记住用户说过的话', diff --git a/src/i18n/zh/index.ts b/src/i18n/zh/index.ts index 918e4550..6bf79c7c 100644 --- a/src/i18n/zh/index.ts +++ b/src/i18n/zh/index.ts @@ -3,11 +3,13 @@ import auth from './auth'; import conversation from './conversation'; import application from './application'; import api from './api'; +import setting from './setting'; export default { common, conversation, auth, application, - api + api, + setting }; diff --git a/src/i18n/zh/setting/field.ts b/src/i18n/zh/setting/field.ts new file mode 100644 index 00000000..1e459bf3 --- /dev/null +++ b/src/i18n/zh/setting/field.ts @@ -0,0 +1,4 @@ +export default { + stream: '流式传输', + endpoint: '服务端点' +}; diff --git a/src/i18n/zh/setting/index.ts b/src/i18n/zh/setting/index.ts new file mode 100644 index 00000000..4d4bf001 --- /dev/null +++ b/src/i18n/zh/setting/index.ts @@ -0,0 +1,3 @@ +import field from './field'; + +export default { field }; diff --git a/src/operators/api/chatgpt/operator.ts b/src/operators/api/chatgpt/operator.ts index f89d97d7..d84d7b18 100644 --- a/src/operators/api/chatgpt/operator.ts +++ b/src/operators/api/chatgpt/operator.ts @@ -5,14 +5,11 @@ export interface IQuery { token: string; } -const baseUrl = 'https://api.zhishuyun.com'; - class ChatGPTOperator { async post(data: IRequest, query: IQuery, config?: AxiosRequestConfig): Promise> { return await axios.post(`/chatgpt`, data, { ...config, - params: query, - baseURL: baseUrl + params: query }); } } diff --git a/src/pages/conversation/Main.vue b/src/pages/conversation/Main.vue index 4297565a..a4dbf55b 100644 --- a/src/pages/conversation/Main.vue +++ b/src/pages/conversation/Main.vue @@ -63,14 +63,22 @@ import { IApplication, IApplicationDetailResponse, IApplicationListResponse, - IApplicationType + IApplicationType, + userOperator } from '@/operators'; import { ROUTE_AUTH_LOGIN, ROUTE_CONVERSATION_DETAIL } from '@/router/constants'; -import { ERROR_CODE_CANCELED, ERROR_CODE_DUPLICATION, ERROR_CODE_UNKNOWN, ERROR_CODE_UNVERIFIED } from '@/constants'; +import { + ENDPOINT, + ERROR_CODE_CANCELED, + ERROR_CODE_DUPLICATION, + ERROR_CODE_UNKNOWN, + ERROR_CODE_UNVERIFIED +} from '@/constants'; import { getVerificationUrl } from '@/utils'; import { IConversation } from '@/operators/conversation/models'; import Introduction from '@/components/conversation/Introduction.vue'; import axios from 'axios'; +import { IResponse } from '@/operators/api/chatgpt/models'; export interface IData { input: string; @@ -81,6 +89,7 @@ export interface IData { api: IApi | undefined; application: IApplication | undefined; canceler: AbortController | undefined; + api_id: string; } export default defineComponent({ @@ -101,7 +110,8 @@ export default defineComponent({ token: undefined, api: undefined, application: undefined, - canceler: undefined + canceler: undefined, + api_id: '1d58971c-e3cd-4713-a3ce-854a731adb14' }; }, computed: { @@ -110,6 +120,9 @@ export default defineComponent({ }, user() { return this.$store.getters.user; + }, + setting() { + return this.$store.getters.setting; } }, watch: { @@ -127,8 +140,14 @@ export default defineComponent({ }); } else { this.restoreMessages(); - await this.onGetApiInfo(); - await this.onCheckApplication(); + this.loading = true; + Promise.all([this.onGetApiInfo(), this.onCheckApplication()]) + .then(() => { + this.loading = false; + }) + .catch(() => { + this.loading = false; + }); } }, methods: { @@ -148,47 +167,30 @@ export default defineComponent({ this.messages = []; } }, - async onGetApiInfo() { - return new Promise((resolve, reject) => { - const id = '1d58971c-e3cd-4713-a3ce-854a731adb14'; - apiOperator - .get(id) - .then(({ data: data }: { data: IApiDetailResponse }) => { - this.api = data; - resolve(data); - }) - .catch((error) => { - reject(error); - }); - }); + async onGetApiInfo(): Promise { + const { data } = await apiOperator.get(this.api_id); + this.api = data; }, async onStop() { this.canceler?.abort(); }, - async onCheckApplication() { + async onCheckApplication(): Promise { if (!this.user) { - return; + const user = await userOperator.getMe(); + await this.$store.dispatch('setUser', user); + } + const { data } = await applicationOperator.getAll({ + user_id: this.user.id, + api_id: this.api_id + }); + if (data.items?.length > 0) { + const application = data.items[0]; + this.application = application; + const token = application.credential?.token; + if (token) { + this.token = token; + } } - this.loading = true; - applicationOperator - .getAll({ - user_id: this.user.id, - api_id: this.api?.id - }) - .then(({ data: data }: { data: IApplicationListResponse }) => { - if (data.items?.length > 0) { - const application = data.items[0]; - this.application = application; - const token = application.credential?.token; - if (token) { - this.token = token; - } - } - this.loading = false; - }) - .catch(() => { - this.loading = false; - }); }, onApply() { applicationOperator @@ -220,6 +222,48 @@ export default defineComponent({ onDraft(content: string) { this.input = content; }, + async onProcessConversation() { + const conversations: IConversation[] = this.$store.getters.conversations; + const hitConversations = conversations.filter((c: IConversation) => c.id === this.conversationId); + if (!hitConversations || hitConversations.length === 0) { + // append a new conversation to conversations + const newConversations = [ + ...conversations, + { + id: this.conversationId, + messages: this.messages, + title: this.conversationId + } + ]; + this.$store.dispatch('setConversations', newConversations); + } else { + // update one of existing conversations + conversations.forEach((conversation: IConversation) => { + if (conversation.id === this.conversationId) { + conversation.messages = this.messages; + } + }); + this.$store.dispatch('setConversations', conversations); + } + }, + async onProcessResponse(jsonData: IResponse) { + const answer = jsonData.answer; + const conversationId = jsonData.conversation_id; + // if this new conversation, just switch to a single conversation + if (conversationId && this.$route.name !== ROUTE_CONVERSATION_DETAIL) { + await this.$router.push({ + name: ROUTE_CONVERSATION_DETAIL, + params: { + id: conversationId + } + }); + } + if (this.conversationId === conversationId) { + this.scrollDown(); + this.messages[this.messages.length - 1].content.value = answer; + this.messages[this.messages.length - 1].state = IMessageState.ANSWERING; + } + }, onSend() { if (this.answering) { return; @@ -277,63 +321,37 @@ export default defineComponent({ }, { headers: { - accept: 'application/x-ndjson', + accept: this.setting?.stream ? 'application/x-ndjson' : 'application/json', 'content-type': 'application/json' }, + baseURL: this.$store.getters.endpoint || ENDPOINT, signal: this.canceler.signal, - responseType: 'stream', - onDownloadProgress: (event) => { - const response = event.target.response; - const lines = response.split('\r\n').filter((line: string) => !!line); - const lastLine = lines[lines.length - 1]; - if (lastLine) { - const jsonData = JSON.parse(lastLine); - const answer = jsonData.answer; - const conversationId = jsonData.conversation_id; - if (conversationId && this.$route.name !== ROUTE_CONVERSATION_DETAIL) { - this.$router.push({ - name: ROUTE_CONVERSATION_DETAIL, - params: { - id: conversationId + // only enable for stream mode + ...(this.setting?.stream + ? { + responseType: 'stream', + onDownloadProgress: (event) => { + const response = event.target.response; + const lines = response.split('\r\n').filter((line: string) => !!line); + const lastLine = lines[lines.length - 1]; + if (lastLine) { + const jsonData = JSON.parse(lastLine); + this.onProcessResponse(jsonData); } - }); + } } - if (this.conversationId === conversationId) { - this.scrollDown(); - this.messages[this.messages.length - 1].content.value = answer; - this.messages[this.messages.length - 1].state = IMessageState.ANSWERING; - } - } - } + : {}) } ) - .then(() => { + .then(async ({ data }: { data: IResponse }) => { + if (!this.setting.stream) { + await this.onProcessResponse(data); + } this.answering = false; if (this.messages && this.messages.length > 0) { this.messages[this.messages.length - 1].state = IMessageState.FINISHED; } - const conversations: IConversation[] = this.$store.getters.conversations; - const hitConversations = conversations.filter((c: IConversation) => c.id === this.conversationId); - if (!hitConversations || hitConversations.length === 0) { - // append a new conversation to conversations - const newConversations = [ - ...conversations, - { - id: this.conversationId, - messages: this.messages, - title: this.conversationId - } - ]; - this.$store.dispatch('setConversations', newConversations); - } else { - // update one of existing conversations - conversations.forEach((conversation: IConversation) => { - if (conversation.id === this.conversationId) { - conversation.messages = this.messages; - } - }); - this.$store.dispatch('setConversations', conversations); - } + this.onProcessConversation(); }) .catch((error) => { this.answering = false; @@ -344,7 +362,7 @@ export default defineComponent({ this.messages[this.messages.length - 1].error = { code: ERROR_CODE_CANCELED }; - } else if (error?.response?.data) { + } else if (error?.response?.data && error?.response?.data.code) { const data = error?.response?.data; if (this.messages && this.messages.length > 0) { this.messages[this.messages.length - 1].error = data; diff --git a/src/plugins/font-awesome.ts b/src/plugins/font-awesome.ts index 7b4829fb..5f7cfc6b 100644 --- a/src/plugins/font-awesome.ts +++ b/src/plugins/font-awesome.ts @@ -9,6 +9,7 @@ import { import { faMicrophone as faSolidMicrophone, faStop as faSolidStop, + faGear as faSolidGear, faRotateRight as faSolidRotateRight, faCheck as faSolidCheck, faPlus as faSolidPlus, @@ -30,6 +31,7 @@ library.add(faSolidCheck); library.add(faRegularUser); library.add(faRegularSun); library.add(faSolidBolt); +library.add(faSolidGear); library.add(faSolidMicrophone); library.add(faSolidPlus); library.add(faSolidQuestion); diff --git a/src/store/index.ts b/src/store/index.ts index 61145161..4cc4bcd7 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -3,12 +3,19 @@ import createPersistedState from 'vuex-persistedstate'; import { userOperator, IUser } from '@/operators'; import { removeCookies } from '@/utils/cookie'; import { IConversation } from '@/operators/conversation/models'; +import { ENDPOINT } from '@/constants'; + +export interface ISetting { + stream?: boolean; + endpoint?: string; +} export interface IState { accessToken: string | undefined; refreshToken: string | undefined; user: IUser | undefined; conversations: IConversation[]; + setting: ISetting; } const store = createStore({ @@ -17,7 +24,11 @@ const store = createStore({ accessToken: undefined, refreshToken: undefined, user: undefined, - conversations: [] + conversations: [], + setting: { + endpoint: ENDPOINT, + stream: false + } }; }, mutations: { @@ -30,6 +41,12 @@ const store = createStore({ setUser(state: IState, payload: IUser): void { state.user = payload; }, + setSetting(state: IState, payload: ISetting): void { + state.setting = { + ...state.setting, + ...payload + }; + }, setConversations(state: IState, payload: IConversation[]): void { state.conversations = payload; } @@ -50,6 +67,9 @@ const store = createStore({ setUser({ commit }: ActionContext, payload: IUser) { commit('setUser', payload); }, + setSetting({ commit }: ActionContext, payload: ISetting) { + commit('setSetting', payload); + }, setConversations({ commit }: ActionContext, payload: IConversation[]) { commit('setConversations', payload); }, @@ -77,6 +97,9 @@ const store = createStore({ user(state): IUser | undefined { return state.user; }, + setting(state): ISetting | undefined { + return state.setting; + }, conversations(state): IConversation[] { return state.conversations || []; }