From 6c68c447c272896ab247e48d86d33f02c2708153 Mon Sep 17 00:00:00 2001 From: AceDataCloud Date: Wed, 4 Oct 2023 12:21:53 +0800 Subject: [PATCH] update fetch history --- src/components/common/ApiStatus.vue | 2 +- src/operators/chat/models.ts | 20 ++++++---- src/operators/chat/operator.ts | 10 ++++- src/pages/chat/Conversation.vue | 60 +++++++++++++++-------------- 4 files changed, 53 insertions(+), 39 deletions(-) diff --git a/src/components/common/ApiStatus.vue b/src/components/common/ApiStatus.vue index 763a0b3d..80c58f1a 100644 --- a/src/components/common/ApiStatus.vue +++ b/src/components/common/ApiStatus.vue @@ -14,7 +14,7 @@ export default defineComponent({ components: {}, props: { application: { - type: Object as () => IApplication, + type: Object as () => IApplication | undefined, required: true } }, diff --git a/src/operators/chat/models.ts b/src/operators/chat/models.ts index 0dd7ab16..dd3fcbb5 100644 --- a/src/operators/chat/models.ts +++ b/src/operators/chat/models.ts @@ -41,8 +41,8 @@ export interface IChatMessage { error?: IError; } -export interface IChatHistory { - conversation_id: string; +export interface IChatConversation { + id: string; messages: IChatMessage[]; } @@ -53,7 +53,7 @@ export interface IChatAskOptions { path: string; } -export interface IChatHistoryOptions { +export interface IChatConversationOptions { endpoint: string; path: string; } @@ -70,9 +70,15 @@ export interface IChatAskResponse { conversation_id?: string; } -export interface IChatHistoryRequest { - action: string; - conversation_id: string; +export enum IChatConversationAction { + RETRIEVE = 'retrieve', + UPDATE = 'update' } -export type IChatHistoryResponse = IChatHistory; +export interface IChatConversationRequest { + action: IChatConversationAction; + id: string; + messages?: IChatMessage[]; +} + +export type IChatConversationResponse = IChatConversation; diff --git a/src/operators/chat/operator.ts b/src/operators/chat/operator.ts index f4113294..33989c10 100644 --- a/src/operators/chat/operator.ts +++ b/src/operators/chat/operator.ts @@ -5,7 +5,10 @@ import { IChatAskOptions, IChatAskRequest, IChatAskResponse, - IChatHistoryOptions + IChatHistoryOptions, + IChatConversationRequest, + IChatConversationOptions, + IChatConversationResponse } from './models'; class ChatOperator { @@ -32,7 +35,10 @@ class ChatOperator { }); } - async history(data: IChatHistoryRequest, options: IChatHistoryOptions): Promise> { + async conversations( + data: IChatConversationRequest, + options: IChatConversationOptions + ): Promise> { return await axios.post(options.path, data, { headers: { 'content-type': 'application/json' diff --git a/src/pages/chat/Conversation.vue b/src/pages/chat/Conversation.vue index 77b43301..960df655 100644 --- a/src/pages/chat/Conversation.vue +++ b/src/pages/chat/Conversation.vue @@ -28,7 +28,8 @@ import { applicationOperator, IApplication, IChatMessageState, - IChatAskResponse + IChatAskResponse, + IChatConversationAction } from '@/operators'; import InputBox from '@/components/chat/InputBox.vue'; import ModelSelector from '@/components/chat/ModelSelector.vue'; @@ -73,8 +74,8 @@ export default defineComponent({ }, async mounted() { console.log('mounted'); - await this.onLoadModel(); - await this.onLoadHistory(); + await this.onFetchModel(); + await this.onFetchHistory(); }, methods: { async onCreateNewConversation() { @@ -83,34 +84,12 @@ export default defineComponent({ name: ROUTE_CHAT_CONVERSATION_NEW }); }, - async onLoadHistory() { - if (!this.conversationId) { - return; - } - const endpoint = this.application?.api?.endpoint; - const path = this.application?.api?.path; - console.log(endpoint, path); - if (!endpoint || !path) { - console.error('no endpoint or path'); - return; - } - const { data: data } = await chatOperator.history( - { - action: 'retrieve', - conversation_id: this.conversationId - }, - { - endpoint, - path: `${path}/history` - } - ); - this.messages = data.messages || []; - }, + async onSelectModel() { await this.onCreateNewConversation(); - await this.onLoadModel(); + await this.onFetchModel(); }, - async onLoadModel() { + async onFetchModel() { this.initializing = true; const { data: applications } = await applicationOperator.getAll({ user_id: this.$store.state.user.id, @@ -131,6 +110,29 @@ export default defineComponent({ this.question = ''; await this.onFetchAnswer(); }, + async onFetchHistory() { + if (!this.conversationId) { + return; + } + const endpoint = this.application?.api?.endpoint; + const path = this.application?.api?.path; + console.log(endpoint, path); + if (!endpoint || !path) { + console.error('no endpoint or path'); + return; + } + const { data: data } = await chatOperator.conversations( + { + action: IChatConversationAction.RETRIEVE, + id: this.conversationId + }, + { + endpoint, + path: `${path}/conversations` + } + ); + this.messages = data.messages || []; + }, async onFetchAnswer() { const token = this.application?.credential?.token; const endpoint = this.application?.api?.endpoint; @@ -176,7 +178,7 @@ export default defineComponent({ ) .then(() => { this.messages[this.messages.length - 1].state = IChatMessageState.FINISHED; - this.onLoadModel(); + this.onFetchModel(); }) .catch((error) => { if (this.messages && this.messages.length > 0) {