From 4458e744ccf757f2bfbfd4a16432ffe44efbc54e Mon Sep 17 00:00:00 2001 From: ChenZhaoYu <790348264@qq.com> Date: Tue, 14 Feb 2023 16:57:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BE=A7=E8=BE=B9=E6=A0=8F=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E5=8F=96=E6=B6=88=E4=B8=8A=E6=AC=A1=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/index.ts | 11 ++++---- src/components/business/Chat/index.vue | 35 ++++++++++++++++++-------- src/store/modules/history/index.ts | 3 ++- src/utils/request/index.ts | 15 ++++++----- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index e0e87d6..4f997d1 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,17 +1,16 @@ +import type { GenericAbortSignal } from 'axios' import { post } from '@/utils/request' +export const controller = new AbortController() + export function fetchChatAPI( prompt: string, options?: { conversationId?: string; parentMessageId?: string }, + signal?: GenericAbortSignal, ) { return post({ url: '/chat', data: { prompt, options }, - }) -} - -export function clearConversations() { - return post({ - url: '/clear', + signal, }) } diff --git a/src/components/business/Chat/index.vue b/src/components/business/Chat/index.vue index dfdceaf..85d3e12 100644 --- a/src/components/business/Chat/index.vue +++ b/src/components/business/Chat/index.vue @@ -1,5 +1,5 @@ @@ -109,7 +122,7 @@ watch( - + diff --git a/src/store/modules/history/index.ts b/src/store/modules/history/index.ts index b2907f8..b9ceb5e 100644 --- a/src/store/modules/history/index.ts +++ b/src/store/modules/history/index.ts @@ -1,7 +1,6 @@ import { defineStore } from 'pinia' import type { HistoryState } from './helper' import { getLocalHistory, setLocalHistory } from './helper' - export const useHistoryStore = defineStore('history-store', { state: (): HistoryState => getLocalHistory(), getters: { @@ -65,6 +64,8 @@ export const useHistoryStore = defineStore('history-store', { }, chooseHistory(index: number) { + if (this.active === index) + return this.active = index setLocalHistory(this.$state) }, diff --git a/src/utils/request/index.ts b/src/utils/request/index.ts index cae8592..2251233 100644 --- a/src/utils/request/index.ts +++ b/src/utils/request/index.ts @@ -1,4 +1,4 @@ -import type { AxiosResponse } from 'axios' +import type { AxiosResponse, GenericAbortSignal } from 'axios' import request from './axios' export interface HttpOption { @@ -6,6 +6,7 @@ export interface HttpOption { data?: any method?: string headers?: any + signal?: GenericAbortSignal beforeRequest?: () => void afterRequest?: () => void } @@ -20,7 +21,7 @@ export interface Response { status: string } -function http({ url, data, method, headers, beforeRequest, afterRequest }: HttpOption) { +function http({ url, data, method, headers, signal, beforeRequest, afterRequest }: HttpOption) { const successHandler = (res: AxiosResponse>) => { if (res.data.status === 'Success') return res.data @@ -40,30 +41,32 @@ function http({ url, data, method, headers, beforeRequest, afterRequest const params = Object.assign(typeof data === 'function' ? data() : data ?? {}, {}) return method === 'GET' - ? request.get(url, { params }).then(successHandler, failHandler) - : request.post(url, params, { headers }).then(successHandler, failHandler) + ? request.get(url, { params, signal }).then(successHandler, failHandler) + : request.post(url, params, { headers, signal }).then(successHandler, failHandler) } export function get( - { url, data, method = 'GET', beforeRequest, afterRequest }: HttpOption, + { url, data, method = 'GET', signal, beforeRequest, afterRequest }: HttpOption, ): Promise> { return http({ url, method, data, + signal, beforeRequest, afterRequest, }) } export function post( - { url, data, method = 'POST', headers, beforeRequest, afterRequest }: HttpOption, + { url, data, method = 'POST', headers, signal, beforeRequest, afterRequest }: HttpOption, ): Promise> { return http({ url, method, data, headers, + signal, beforeRequest, afterRequest, })