From 27ba9330995193e1572ea52b6dd541d6285c2179 Mon Sep 17 00:00:00 2001 From: ningyv <1793599591@qq.com> Date: Mon, 17 Feb 2025 16:33:49 +0800 Subject: [PATCH] feat: feature/1.5-Data Integration --- frontend/packages/common/src/hooks/http.ts | 12 +++++++++++- .../core/src/pages/aiSetting/LocalModelList.tsx | 9 ++++++++- .../packages/core/src/pages/system/SystemList.tsx | 9 ++++++++- .../system/serviceDeployment/ServiceDeployment.tsx | 7 +++++-- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/frontend/packages/common/src/hooks/http.ts b/frontend/packages/common/src/hooks/http.ts index 92eb99a0..06a23f64 100644 --- a/frontend/packages/common/src/hooks/http.ts +++ b/frontend/packages/common/src/hooks/http.ts @@ -136,6 +136,7 @@ type EoRequest = RequestInit & { eoBody?: { [k: string]: unknown } | Array | string isStream?: boolean handleStream?: (line: any) => void + callback?: (cancel: () => void) => void } type EoHeaders = Headers | { [k: string]: string } @@ -145,6 +146,14 @@ export function useFetch() { const pluginEventHub = usePluginEventHub() function fetchData(url: string, options: EoRequest) { + const controller = new AbortController() + const signal = controller.signal + + // 如果提供了callback,则传递取消请求的函数 + if (options.callback) { + options.callback(() => controller.abort()) + } + // 合并传入的headers与默认headers const headers = { ...(options.body ? {} : DEFAULT_HEADERS), ...options.headers } @@ -163,7 +172,8 @@ export function useFetch() { headers: { ...headers // Authorization: 'Bearer your-token', // 示例:添加统一的Token认证 - } + }, + signal // 将signal传递给fetch请求 } return fetch(`${options?.eoApiPrefix === undefined ? '/api/v1/' : options.eoApiPrefix}${url}`, finalOptions) diff --git a/frontend/packages/core/src/pages/aiSetting/LocalModelList.tsx b/frontend/packages/core/src/pages/aiSetting/LocalModelList.tsx index 1b5e6c1c..4262e24d 100644 --- a/frontend/packages/core/src/pages/aiSetting/LocalModelList.tsx +++ b/frontend/packages/core/src/pages/aiSetting/LocalModelList.tsx @@ -277,12 +277,19 @@ const LocalModelList: React.FC = () => { record.state = 'error' modalInstance.update({}) } + let cancelCb: () => void = () => {} + const cancel = (cancel: () => void) => { + cancelCb = cancel + } const modalInstance = modal.confirm({ title: $t('部署过程'), - content: , + content: , footer: () => { return }, + afterClose: () => { + cancelCb() + }, width: 600, okText: $t('确认'), cancelText: $t('取消'), diff --git a/frontend/packages/core/src/pages/system/SystemList.tsx b/frontend/packages/core/src/pages/system/SystemList.tsx index 6d4c630a..4e0f5f12 100644 --- a/frontend/packages/core/src/pages/system/SystemList.tsx +++ b/frontend/packages/core/src/pages/system/SystemList.tsx @@ -145,12 +145,19 @@ const SystemList: FC = () => { record.state = 'error' modalInstance.update({}) } + let cancelCb: () => void = () => {} + const cancel = (cancel: () => void) => { + cancelCb = cancel + } const modalInstance = modal.confirm({ title: $t('部署过程'), - content: , + content: , footer: () => { return }, + afterClose: () => { + cancelCb() + }, width: 600, okText: $t('确认'), cancelText: $t('取消'), diff --git a/frontend/packages/core/src/pages/system/serviceDeployment/ServiceDeployment.tsx b/frontend/packages/core/src/pages/system/serviceDeployment/ServiceDeployment.tsx index ca63097a..6a2dd88f 100644 --- a/frontend/packages/core/src/pages/system/serviceDeployment/ServiceDeployment.tsx +++ b/frontend/packages/core/src/pages/system/serviceDeployment/ServiceDeployment.tsx @@ -8,8 +8,8 @@ import { $t } from '@common/locales/index.ts' import { useFetch } from '@common/hooks/http' import { BasicResponse, RESPONSE_TIPS, STATUS_CODE } from '@common/const/const' -export const ServiceDeployment = (props: { record: SystemTableListItem, closeModal?: () => void, updateFooter?: () => void }) => { - const { record, closeModal, updateFooter } = props +export const ServiceDeployment = (props: { record: SystemTableListItem, closeModal?: () => void, updateFooter?: () => void, cancelCb?: (cancel: () => void) => void }) => { + const { record, closeModal, updateFooter, cancelCb } = props const { message } = App.useApp() const getIcon = (status: string) => { switch (status) { @@ -126,6 +126,9 @@ export const ServiceDeployment = (props: { record: SystemTableListItem, closeMod method: 'POST', eoBody: { model: record.id, team: record.team?.id }, isStream: true, + callback: (cancel: () => void) => { + cancelCb?.(cancel) + }, handleStream: (chunk) => { const parsedChunk = JSON.parse(chunk) // 下载中