mirror of
https://github.com/langgenius/dify.git
synced 2026-10-16 15:35:32 +08:00
refactor(web): migrate plugin console calls in use-plugins to the console contract
- install/upgrade/upload, permission and auto-upgrade settings, debugging key, tasks and dynamic options call consoleClient; plugin UI DTOs are narrowed at the boundary - extract the boundary narrowing helper to service/console/narrow-response - dynamic options default provider_type to 'tool' (the backend requires it) - marketplace (external API) calls stay on getMarketplace/postMarketplace Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d7892875af
commit
64d03e72c7
@@ -379,12 +379,6 @@ export type uploadGitHubResponse = {
|
||||
manifest: PluginDeclaration
|
||||
}
|
||||
|
||||
export type DebugInfo = {
|
||||
key: string
|
||||
host: string
|
||||
port: number
|
||||
}
|
||||
|
||||
export const TaskStatus = {
|
||||
pending: 'pending',
|
||||
running: 'running',
|
||||
|
||||
@@ -49,12 +49,20 @@ vi.mock('@/service/base', () => ({
|
||||
request: mockRequest,
|
||||
}))
|
||||
|
||||
mockRequest.mockImplementation(async (url: string) => {
|
||||
const data = await mockGet(url)
|
||||
return new Response(JSON.stringify(data), {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
})
|
||||
mockRequest.mockImplementation(
|
||||
async (url: string, _init: RequestInit, options: { request?: Request }) => {
|
||||
const method = options.request?.method ?? 'GET'
|
||||
const data =
|
||||
method === 'GET'
|
||||
? await mockGet(url)
|
||||
: await mockPost(new URL(url).pathname.replace('/console/api', ''), {
|
||||
body: await options.request?.json(),
|
||||
})
|
||||
return new Response(JSON.stringify(data ?? {}), {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
},
|
||||
)
|
||||
|
||||
vi.mock('@/app/components/plugins/install-plugin/hooks/use-refresh-plugin-list', () => ({
|
||||
default: () => ({
|
||||
@@ -1346,7 +1354,7 @@ describe('usePluginAutoUpgradeSettings', () => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('does not expose frontend default settings before backend data resolves', () => {
|
||||
it('does not expose frontend default settings before backend data resolves', async () => {
|
||||
const queryClient = createQueryClient()
|
||||
mockGet.mockReturnValue(new Promise(() => {}))
|
||||
|
||||
@@ -1355,10 +1363,10 @@ describe('usePluginAutoUpgradeSettings', () => {
|
||||
})
|
||||
|
||||
expect(result.current.data).toBeUndefined()
|
||||
expect(mockGet).toHaveBeenCalledWith('/workspaces/current/plugin/auto-upgrade/fetch', {
|
||||
params: {
|
||||
category: PluginCategoryEnum.model,
|
||||
},
|
||||
await waitFor(() => {
|
||||
expect(mockGet).toHaveBeenCalledWith(
|
||||
`http://localhost:5001/console/api/workspaces/current/plugin/auto-upgrade/fetch?category=${PluginCategoryEnum.model}`,
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Some UI types narrow fields that backend response models declare as plain strings or
|
||||
* loose records (enum-like statuses, `doc_form`, plugin `source`, locale label maps, ...).
|
||||
* Until those response models declare literal types, contract responses are narrowed to the
|
||||
* UI types at the service boundary with this helper instead of re-typing every consumer.
|
||||
* Prefer aliasing the generated type whenever the UI type can follow it.
|
||||
*/
|
||||
export const narrowResponse = <T>(response: Promise<unknown>) => response as Promise<T>
|
||||
+15
-22
@@ -23,14 +23,7 @@ import { DataSourceProvider } from '@/models/common'
|
||||
// oxlint-disable-next-line no-restricted-imports -- the zip download needs the raw Blob response
|
||||
import { post } from './base'
|
||||
import { consoleClient } from './console'
|
||||
|
||||
/**
|
||||
* The knowledge UI types in `@/models/datasets` narrow fields that backend response models
|
||||
* declare as plain strings (indexing_technique, doc_form, permission, indexing statuses, ...).
|
||||
* Until those response models declare literal types, contract responses are narrowed to the UI
|
||||
* types at this boundary instead of re-typing every consumer.
|
||||
*/
|
||||
export const narrowKnowledgeResponse = <T>(response: Promise<unknown>) => response as Promise<T>
|
||||
import { narrowResponse } from './console/narrow-response'
|
||||
|
||||
// apis for documents in a dataset
|
||||
|
||||
@@ -64,17 +57,17 @@ export const updateDatasetSetting = ({
|
||||
datasetId: string
|
||||
body: DatasetUpdatePayload
|
||||
}): Promise<DataSet> => {
|
||||
return narrowKnowledgeResponse(
|
||||
return narrowResponse(
|
||||
consoleClient.datasets.byDatasetId.patch({ params: { dataset_id: datasetId }, body }),
|
||||
)
|
||||
}
|
||||
|
||||
export const fetchDatasets = ({ params }: FetchDatasetsParams): Promise<DataSetListResponse> => {
|
||||
return narrowKnowledgeResponse(consoleClient.datasets.get({ query: params }))
|
||||
return narrowResponse(consoleClient.datasets.get({ query: params }))
|
||||
}
|
||||
|
||||
export const createEmptyDataset = ({ name }: { name: string }): Promise<DataSet> => {
|
||||
return narrowKnowledgeResponse(consoleClient.datasets.post({ body: { name } }))
|
||||
return narrowResponse(consoleClient.datasets.post({ body: { name } }))
|
||||
}
|
||||
|
||||
export const checkIsUsedInApp = (id: string): Promise<{ is_using: boolean }> => {
|
||||
@@ -93,7 +86,7 @@ export const fetchExternalAPI = ({
|
||||
}: {
|
||||
apiTemplateId: string
|
||||
}): Promise<ExternalAPIItem> => {
|
||||
return narrowKnowledgeResponse(
|
||||
return narrowResponse(
|
||||
consoleClient.datasets.externalKnowledgeApi.byExternalKnowledgeApiId.get({
|
||||
params: { external_knowledge_api_id: apiTemplateId },
|
||||
}),
|
||||
@@ -107,7 +100,7 @@ export const updateExternalAPI = ({
|
||||
apiTemplateId: string
|
||||
body: ExternalAPIItem
|
||||
}): Promise<ExternalAPIItem> => {
|
||||
return narrowKnowledgeResponse(
|
||||
return narrowResponse(
|
||||
consoleClient.datasets.externalKnowledgeApi.byExternalKnowledgeApiId.patch({
|
||||
params: { external_knowledge_api_id: apiTemplateId },
|
||||
body,
|
||||
@@ -136,7 +129,7 @@ export const createExternalAPI = ({
|
||||
}: {
|
||||
body: CreateExternalAPIReq
|
||||
}): Promise<ExternalAPIItem> => {
|
||||
return narrowKnowledgeResponse(consoleClient.datasets.externalKnowledgeApi.post({ body }))
|
||||
return narrowResponse(consoleClient.datasets.externalKnowledgeApi.post({ body }))
|
||||
}
|
||||
|
||||
export const createExternalKnowledgeBase = ({
|
||||
@@ -144,11 +137,11 @@ export const createExternalKnowledgeBase = ({
|
||||
}: {
|
||||
body: CreateKnowledgeBaseReq
|
||||
}): Promise<ExternalKnowledgeItem> => {
|
||||
return narrowKnowledgeResponse(consoleClient.datasets.external.post({ body }))
|
||||
return narrowResponse(consoleClient.datasets.external.post({ body }))
|
||||
}
|
||||
|
||||
export const fetchDefaultProcessRule = (): Promise<ProcessRuleResponse> => {
|
||||
return narrowKnowledgeResponse(consoleClient.datasets.processRule.get({}))
|
||||
return narrowResponse(consoleClient.datasets.processRule.get({}))
|
||||
}
|
||||
|
||||
export const createFirstDocument = ({
|
||||
@@ -156,7 +149,7 @@ export const createFirstDocument = ({
|
||||
}: {
|
||||
body: KnowledgeConfig
|
||||
}): Promise<createDocumentResponse> => {
|
||||
return narrowKnowledgeResponse(consoleClient.datasets.init.post({ body }))
|
||||
return narrowResponse(consoleClient.datasets.init.post({ body }))
|
||||
}
|
||||
|
||||
export const createDocument = ({
|
||||
@@ -166,7 +159,7 @@ export const createDocument = ({
|
||||
datasetId: string
|
||||
body: KnowledgeConfig
|
||||
}): Promise<createDocumentResponse> => {
|
||||
return narrowKnowledgeResponse(
|
||||
return narrowResponse(
|
||||
consoleClient.datasets.byDatasetId.documents.post({ params: { dataset_id: datasetId }, body }),
|
||||
)
|
||||
}
|
||||
@@ -175,7 +168,7 @@ export const fetchIndexingStatus = ({
|
||||
datasetId,
|
||||
documentId,
|
||||
}: CommonDocReq): Promise<IndexingStatusResponse> => {
|
||||
return narrowKnowledgeResponse(
|
||||
return narrowResponse(
|
||||
consoleClient.datasets.byDatasetId.documents.byDocumentId.indexingStatus.get({
|
||||
params: { dataset_id: datasetId, document_id: documentId },
|
||||
}),
|
||||
@@ -186,7 +179,7 @@ export const fetchIndexingStatusBatch = ({
|
||||
datasetId,
|
||||
batchId,
|
||||
}: BatchReq): Promise<IndexingStatusBatchResponse> => {
|
||||
return narrowKnowledgeResponse(
|
||||
return narrowResponse(
|
||||
consoleClient.datasets.byDatasetId.batch.byBatch.indexingStatus.get({
|
||||
params: { dataset_id: datasetId, batch: batchId },
|
||||
}),
|
||||
@@ -198,7 +191,7 @@ export const renameDocumentName = ({
|
||||
documentId,
|
||||
name,
|
||||
}: CommonDocReq & { name: string }): Promise<CommonResponse> => {
|
||||
return narrowKnowledgeResponse(
|
||||
return narrowResponse(
|
||||
consoleClient.datasets.byDatasetId.documents.byDocumentId.rename.post({
|
||||
params: { dataset_id: datasetId, document_id: documentId },
|
||||
body: { name },
|
||||
@@ -244,7 +237,7 @@ export const downloadDocumentsZip = ({
|
||||
export const fetchFileIndexingEstimate = (
|
||||
body: IndexingEstimatePayload,
|
||||
): Promise<FileIndexingEstimateResponse> => {
|
||||
return narrowKnowledgeResponse(consoleClient.datasets.indexingEstimate.post({ body }))
|
||||
return narrowResponse(consoleClient.datasets.indexingEstimate.post({ body }))
|
||||
}
|
||||
|
||||
export const fetchNotionPagePreview = ({
|
||||
|
||||
@@ -14,7 +14,8 @@ import type {
|
||||
import { ORPCError } from '@orpc/client'
|
||||
import { keepPreviousData, useInfiniteQuery, useMutation, useQuery } from '@tanstack/react-query'
|
||||
import { consoleClient } from '../console'
|
||||
import { fetchIndexingStatusBatch, narrowKnowledgeResponse } from '../datasets'
|
||||
import { narrowResponse } from '../console/narrow-response'
|
||||
import { fetchIndexingStatusBatch } from '../datasets'
|
||||
import { useInvalid } from '../use-base'
|
||||
|
||||
const NAME_SPACE = 'dataset'
|
||||
@@ -51,7 +52,7 @@ export const useInfiniteDatasets = (
|
||||
return useInfiniteQuery<DataSetListResponse>({
|
||||
queryKey: [...datasetListQueryKey, 'infinite', normalizedParams],
|
||||
queryFn: ({ pageParam = normalizedParams.page }) =>
|
||||
narrowKnowledgeResponse(
|
||||
narrowResponse(
|
||||
consoleClient.datasets.get({
|
||||
query: { ...normalizedParams, page: pageParam as number | undefined },
|
||||
}),
|
||||
@@ -69,7 +70,7 @@ export const useDatasetList = (params: DatasetListRequest) => {
|
||||
return useInfiniteQuery({
|
||||
queryKey: [...datasetListQueryKey, initialPage, tag_ids, limit, include_all, keyword],
|
||||
queryFn: ({ pageParam = 1 }) =>
|
||||
narrowKnowledgeResponse<DataSetListResponse>(
|
||||
narrowResponse<DataSetListResponse>(
|
||||
consoleClient.datasets.get({
|
||||
query: { tag_ids, limit, include_all, keyword, page: pageParam },
|
||||
}),
|
||||
@@ -90,7 +91,7 @@ export const useDatasetDetail = (datasetId: string) => {
|
||||
return useQuery({
|
||||
queryKey: [...datasetDetailQueryKeyPrefix, datasetId],
|
||||
queryFn: () =>
|
||||
narrowKnowledgeResponse<DataSet>(
|
||||
narrowResponse<DataSet>(
|
||||
consoleClient.datasets.byDatasetId.get({ params: { dataset_id: datasetId } }),
|
||||
),
|
||||
enabled: !!datasetId,
|
||||
@@ -106,7 +107,7 @@ export const useDatasetRelatedApps = (datasetId: string, options?: { enabled?: b
|
||||
return useQuery({
|
||||
queryKey: [NAME_SPACE, 'related-apps', datasetId],
|
||||
queryFn: () =>
|
||||
narrowKnowledgeResponse<RelatedAppResponse>(
|
||||
narrowResponse<RelatedAppResponse>(
|
||||
consoleClient.datasets.byDatasetId.relatedApps.get({ params: { dataset_id: datasetId } }),
|
||||
),
|
||||
enabled: options?.enabled ?? !!datasetId,
|
||||
@@ -129,7 +130,7 @@ export const useProcessRule = (documentId?: string) => {
|
||||
return useQuery<ProcessRuleResponse>({
|
||||
queryKey: [NAME_SPACE, 'process-rule', documentId],
|
||||
queryFn: () =>
|
||||
narrowKnowledgeResponse(
|
||||
narrowResponse(
|
||||
consoleClient.datasets.processRule.get({ query: { document_id: documentId } }),
|
||||
),
|
||||
enabled: !!documentId,
|
||||
@@ -172,7 +173,7 @@ export const useDatasetTestingRecords = (
|
||||
return useQuery<HitTestingRecordsResponse>({
|
||||
queryKey: [NAME_SPACE, 'testing-records', datasetId, params],
|
||||
queryFn: () =>
|
||||
narrowKnowledgeResponse(
|
||||
narrowResponse(
|
||||
consoleClient.datasets.byDatasetId.queries.get({
|
||||
params: { dataset_id: datasetId! },
|
||||
query: params,
|
||||
@@ -187,7 +188,7 @@ export const useDatasetErrorDocs = (datasetId?: string) => {
|
||||
return useQuery<ErrorDocsResponse>({
|
||||
queryKey: [NAME_SPACE, 'error-docs', datasetId],
|
||||
queryFn: () =>
|
||||
narrowKnowledgeResponse(
|
||||
narrowResponse(
|
||||
consoleClient.datasets.byDatasetId.errorDocs.get({ params: { dataset_id: datasetId! } }),
|
||||
),
|
||||
enabled: !!datasetId,
|
||||
|
||||
@@ -9,10 +9,10 @@ import { keepPreviousData, useMutation, useQuery } from '@tanstack/react-query'
|
||||
import { normalizeStatusForQuery } from '@/app/components/datasets/documents/status-filter'
|
||||
import { DocumentActionType } from '@/models/datasets'
|
||||
import { consoleClient } from '../console'
|
||||
import { narrowResponse } from '../console/narrow-response'
|
||||
import {
|
||||
downloadDocumentsZip,
|
||||
fetchDocumentDownloadUrl,
|
||||
narrowKnowledgeResponse,
|
||||
pauseDocIndexing,
|
||||
resumeDocIndexing,
|
||||
retryErrorDocs,
|
||||
@@ -48,7 +48,7 @@ export const useDocumentList = (payload: {
|
||||
return useQuery<DocumentListResponse>({
|
||||
queryKey: [...useDocumentListKey, datasetId, params],
|
||||
queryFn: () =>
|
||||
narrowKnowledgeResponse(
|
||||
narrowResponse(
|
||||
consoleClient.datasets.byDatasetId.documents.get({
|
||||
params: { dataset_id: datasetId },
|
||||
query: params,
|
||||
@@ -165,7 +165,7 @@ export const useDocumentDetail = (payload: {
|
||||
return useQuery<DocumentDetailResponse>({
|
||||
queryKey: [...useDocumentDetailKey, 'withoutMetaData', datasetId, documentId, params],
|
||||
queryFn: () =>
|
||||
narrowKnowledgeResponse(
|
||||
narrowResponse(
|
||||
consoleClient.datasets.byDatasetId.documents.byDocumentId.get({
|
||||
params: { dataset_id: datasetId, document_id: documentId },
|
||||
query: params,
|
||||
@@ -184,7 +184,7 @@ export const useDocumentMetadata = (payload: {
|
||||
return useQuery<DocumentDetailResponse>({
|
||||
queryKey: [...useDocumentDetailKey, 'onlyMetaData', datasetId, documentId, params],
|
||||
queryFn: () =>
|
||||
narrowKnowledgeResponse(
|
||||
narrowResponse(
|
||||
consoleClient.datasets.byDatasetId.documents.byDocumentId.get({
|
||||
params: { dataset_id: datasetId, document_id: documentId },
|
||||
query: params,
|
||||
|
||||
@@ -6,7 +6,7 @@ import type {
|
||||
import type { DocumentDetailResponse } from '@/models/datasets'
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { consoleClient } from '../console'
|
||||
import { narrowKnowledgeResponse } from '../datasets'
|
||||
import { narrowResponse } from '../console/narrow-response'
|
||||
import { useInvalid } from '../use-base'
|
||||
import { useDocumentListKey, useInvalidDocumentList } from './use-document'
|
||||
|
||||
@@ -17,7 +17,7 @@ export const useDatasetMetaData = (datasetId: string) => {
|
||||
{
|
||||
queryKey: [NAME_SPACE, 'dataset', datasetId],
|
||||
queryFn: () => {
|
||||
return narrowKnowledgeResponse(
|
||||
return narrowResponse(
|
||||
consoleClient.datasets.byDatasetId.metadata.get({ params: { dataset_id: datasetId } }),
|
||||
)
|
||||
},
|
||||
@@ -99,7 +99,7 @@ export const useBuiltInMetaDataFields = () => {
|
||||
return useQuery<{ fields: BuiltInMetadataItem[] }>({
|
||||
queryKey: [NAME_SPACE, 'built-in'],
|
||||
queryFn: () => {
|
||||
return narrowKnowledgeResponse(consoleClient.datasets.metadata.builtIn.get())
|
||||
return narrowResponse(consoleClient.datasets.metadata.builtIn.get())
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -114,7 +114,7 @@ export const useDocumentMetaData = ({
|
||||
return useQuery<DocumentDetailResponse>({
|
||||
queryKey: [NAME_SPACE, 'document', datasetId, documentId],
|
||||
queryFn: () => {
|
||||
return narrowKnowledgeResponse(
|
||||
return narrowResponse(
|
||||
consoleClient.datasets.byDatasetId.documents.byDocumentId.get({
|
||||
params: { dataset_id: datasetId, document_id: documentId },
|
||||
query: { metadata: 'only' },
|
||||
|
||||
@@ -9,7 +9,7 @@ import type {
|
||||
} from '@/models/datasets'
|
||||
import { useMutation, useQuery } from '@tanstack/react-query'
|
||||
import { consoleClient } from '../console'
|
||||
import { narrowKnowledgeResponse } from '../datasets'
|
||||
import { narrowResponse } from '../console/narrow-response'
|
||||
|
||||
const NAME_SPACE = 'segment'
|
||||
|
||||
@@ -36,7 +36,7 @@ export const useSegmentList = (
|
||||
return useQuery<SegmentsResponse>({
|
||||
queryKey: [...useSegmentListKey, datasetId, documentId, params],
|
||||
queryFn: () => {
|
||||
return narrowKnowledgeResponse(
|
||||
return narrowResponse(
|
||||
consoleClient.datasets.byDatasetId.documents.byDocumentId.segments.get({
|
||||
params: { dataset_id: datasetId, document_id: documentId },
|
||||
query: { ...params, enabled: String(params.enabled) },
|
||||
@@ -57,7 +57,7 @@ export const useUpdateSegment = () => {
|
||||
body: SegmentUpdater
|
||||
}) => {
|
||||
const { datasetId, documentId, segmentId, body } = payload
|
||||
return narrowKnowledgeResponse<{ data: SegmentDetailModel; doc_form: ChunkingMode }>(
|
||||
return narrowResponse<{ data: SegmentDetailModel; doc_form: ChunkingMode }>(
|
||||
consoleClient.datasets.byDatasetId.documents.byDocumentId.segments.bySegmentId.patch({
|
||||
params: { dataset_id: datasetId, document_id: documentId, segment_id: segmentId },
|
||||
body,
|
||||
@@ -72,7 +72,7 @@ export const useAddSegment = () => {
|
||||
mutationKey: [NAME_SPACE, 'add'],
|
||||
mutationFn: (payload: { datasetId: string; documentId: string; body: SegmentUpdater }) => {
|
||||
const { datasetId, documentId, body } = payload
|
||||
return narrowKnowledgeResponse<{ data: SegmentDetailModel; doc_form: ChunkingMode }>(
|
||||
return narrowResponse<{ data: SegmentDetailModel; doc_form: ChunkingMode }>(
|
||||
consoleClient.datasets.byDatasetId.documents.byDocumentId.segment.post({
|
||||
params: { dataset_id: datasetId, document_id: documentId },
|
||||
body,
|
||||
@@ -141,7 +141,7 @@ export const useChildSegmentList = (
|
||||
return useQuery({
|
||||
queryKey: [...useChildSegmentListKey, datasetId, documentId, segmentId, params],
|
||||
queryFn: () => {
|
||||
return narrowKnowledgeResponse<ChildSegmentsResponse>(
|
||||
return narrowResponse<ChildSegmentsResponse>(
|
||||
consoleClient.datasets.byDatasetId.documents.byDocumentId.segments.bySegmentId.childChunks.get(
|
||||
{
|
||||
params: { dataset_id: datasetId, document_id: documentId, segment_id: segmentId },
|
||||
@@ -188,7 +188,7 @@ export const useAddChildSegment = () => {
|
||||
body: { content: string }
|
||||
}) => {
|
||||
const { datasetId, documentId, segmentId, body } = payload
|
||||
return narrowKnowledgeResponse<{ data: ChildChunkDetail }>(
|
||||
return narrowResponse<{ data: ChildChunkDetail }>(
|
||||
consoleClient.datasets.byDatasetId.documents.byDocumentId.segments.bySegmentId.childChunks.post(
|
||||
{
|
||||
params: { dataset_id: datasetId, document_id: documentId, segment_id: segmentId },
|
||||
@@ -211,7 +211,7 @@ export const useUpdateChildSegment = () => {
|
||||
body: { content: string }
|
||||
}) => {
|
||||
const { datasetId, documentId, segmentId, childChunkId, body } = payload
|
||||
return narrowKnowledgeResponse<{ data: ChildChunkDetail }>(
|
||||
return narrowResponse<{ data: ChildChunkDetail }>(
|
||||
consoleClient.datasets.byDatasetId.documents.byDocumentId.segments.bySegmentId.childChunks.byChildChunkId.patch(
|
||||
{
|
||||
params: {
|
||||
@@ -237,7 +237,7 @@ export const useSegmentBatchImport = () => {
|
||||
body: { upload_file_id: string }
|
||||
}) => {
|
||||
const { datasetId, documentId, body } = payload
|
||||
return narrowKnowledgeResponse<BatchImportResponse>(
|
||||
return narrowResponse<BatchImportResponse>(
|
||||
consoleClient.datasets.byDatasetId.documents.byDocumentId.segments.batchImport.post({
|
||||
params: { dataset_id: datasetId, document_id: documentId },
|
||||
body,
|
||||
@@ -252,7 +252,7 @@ export const useCheckSegmentBatchImportProgress = () => {
|
||||
mutationKey: [NAME_SPACE, 'batchImport', 'checkProgress'],
|
||||
mutationFn: (payload: { jobID: string }) => {
|
||||
const { jobID } = payload
|
||||
return narrowKnowledgeResponse<BatchImportResponse>(
|
||||
return narrowResponse<BatchImportResponse>(
|
||||
consoleClient.datasets.batchImportStatus.byJobId.get({ params: { job_id: jobID } }),
|
||||
)
|
||||
},
|
||||
|
||||
+86
-74
@@ -1,5 +1,6 @@
|
||||
import type {
|
||||
GetWorkspacesCurrentPluginByCategoryListData,
|
||||
ParserMarketplaceUpgrade,
|
||||
PluginCategoryInstalledPluginResponse,
|
||||
PluginCategoryListResponse,
|
||||
PluginEntity,
|
||||
@@ -15,7 +16,6 @@ import type { InfiniteData, MutateOptions, QueryClient, QueryOptions } from '@ta
|
||||
import type { FormOption } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import type { AutoUpdateConfig } from '@/app/components/plugins/reference-setting-modal/auto-update-setting/types'
|
||||
import type {
|
||||
DebugInfo as DebugInfoTypes,
|
||||
Dependency,
|
||||
GitHubItemAndMarketPlaceDependency,
|
||||
InstallPackageResponse,
|
||||
@@ -48,11 +48,12 @@ import useRefreshPluginList from '@/app/components/plugins/install-plugin/hooks/
|
||||
import { getFormattedPlugin } from '@/app/components/plugins/marketplace/utils'
|
||||
import { PluginCategoryEnum, PluginSource, TaskStatus } from '@/app/components/plugins/types'
|
||||
import { workspacePermissionKeysAtom } from '@/context/permission-state'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
import { consoleClient, consoleQuery } from '@/service/console'
|
||||
import { narrowResponse } from '@/service/console/narrow-response'
|
||||
import { uninstallPlugin } from '@/service/plugins'
|
||||
import { hasPermission } from '@/utils/permission'
|
||||
// oxlint-disable-next-line no-restricted-imports
|
||||
import { get, getMarketplace, post, postMarketplace } from './base'
|
||||
import { getMarketplace, postMarketplace } from './base'
|
||||
import { useInvalidateAllBuiltInTools } from './use-tools'
|
||||
|
||||
const NAME_SPACE = 'plugins'
|
||||
@@ -809,9 +810,11 @@ export const useInstallPackageFromMarketPlace = (
|
||||
return useMutation({
|
||||
...options,
|
||||
mutationFn: (uniqueIdentifier: string) => {
|
||||
return post<InstallPackageResponse>('/workspaces/current/plugin/install/marketplace', {
|
||||
body: { plugin_unique_identifiers: [uniqueIdentifier] },
|
||||
})
|
||||
return narrowResponse<InstallPackageResponse>(
|
||||
consoleClient.workspaces.current.plugin.install.marketplace.post({
|
||||
body: { plugin_unique_identifiers: [uniqueIdentifier] },
|
||||
}),
|
||||
)
|
||||
},
|
||||
onSuccess: (data, variables, context, mutation) => {
|
||||
upsertStartedPluginTask(queryClient, data)
|
||||
@@ -821,15 +824,17 @@ export const useInstallPackageFromMarketPlace = (
|
||||
}
|
||||
|
||||
export const useUpdatePackageFromMarketPlace = (
|
||||
options?: MutateOptions<InstallPackageResponse, Error, object>,
|
||||
options?: MutateOptions<InstallPackageResponse, Error, ParserMarketplaceUpgrade>,
|
||||
) => {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
...options,
|
||||
mutationFn: (body: object) => {
|
||||
return post<InstallPackageResponse>('/workspaces/current/plugin/upgrade/marketplace', {
|
||||
body,
|
||||
})
|
||||
mutationFn: (body: ParserMarketplaceUpgrade) => {
|
||||
return narrowResponse<InstallPackageResponse>(
|
||||
consoleClient.workspaces.current.plugin.upgrade.marketplace.post({
|
||||
body,
|
||||
}),
|
||||
)
|
||||
},
|
||||
onSuccess: (data, variables, context, mutation) => {
|
||||
upsertStartedPluginTask(queryClient, data)
|
||||
@@ -842,9 +847,11 @@ export const usePluginDeclarationFromMarketPlace = (pluginUniqueIdentifier: stri
|
||||
return useQuery({
|
||||
queryKey: [NAME_SPACE, 'pluginDeclaration', pluginUniqueIdentifier],
|
||||
queryFn: () =>
|
||||
get<{ manifest: PluginDeclaration }>('/workspaces/current/plugin/marketplace/pkg', {
|
||||
params: { plugin_unique_identifier: pluginUniqueIdentifier },
|
||||
}),
|
||||
narrowResponse<{ manifest: PluginDeclaration }>(
|
||||
consoleClient.workspaces.current.plugin.marketplace.pkg.get({
|
||||
query: { plugin_unique_identifier: pluginUniqueIdentifier },
|
||||
}),
|
||||
),
|
||||
enabled: !!pluginUniqueIdentifier,
|
||||
})
|
||||
}
|
||||
@@ -864,9 +871,11 @@ export const useInstallPackageFromLocal = () => {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (uniqueIdentifier: string) => {
|
||||
return post<InstallPackageResponse>('/workspaces/current/plugin/install/pkg', {
|
||||
body: { plugin_unique_identifiers: [uniqueIdentifier] },
|
||||
})
|
||||
return narrowResponse<InstallPackageResponse>(
|
||||
consoleClient.workspaces.current.plugin.install.pkg.post({
|
||||
body: { plugin_unique_identifiers: [uniqueIdentifier] },
|
||||
}),
|
||||
)
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
upsertStartedPluginTask(queryClient, data)
|
||||
@@ -888,14 +897,16 @@ export const useInstallPackageFromGitHub = () => {
|
||||
selectedPackage: string
|
||||
uniqueIdentifier: string
|
||||
}) => {
|
||||
return post<InstallPackageResponse>('/workspaces/current/plugin/install/github', {
|
||||
body: {
|
||||
repo: repoUrl,
|
||||
version: selectedVersion,
|
||||
package: selectedPackage,
|
||||
plugin_unique_identifier: uniqueIdentifier,
|
||||
},
|
||||
})
|
||||
return narrowResponse<InstallPackageResponse>(
|
||||
consoleClient.workspaces.current.plugin.install.github.post({
|
||||
body: {
|
||||
repo: repoUrl,
|
||||
version: selectedVersion,
|
||||
package: selectedPackage,
|
||||
plugin_unique_identifier: uniqueIdentifier,
|
||||
},
|
||||
}),
|
||||
)
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
upsertStartedPluginTask(queryClient, data)
|
||||
@@ -907,9 +918,11 @@ export const useUploadGitHub = (payload: { repo: string; version: string; packag
|
||||
return useQuery({
|
||||
queryKey: [NAME_SPACE, 'uploadGitHub', payload],
|
||||
queryFn: () =>
|
||||
post<uploadGitHubResponse>('/workspaces/current/plugin/upload/github', {
|
||||
body: payload,
|
||||
}),
|
||||
narrowResponse<uploadGitHubResponse>(
|
||||
consoleClient.workspaces.current.plugin.upload.github.post({
|
||||
body: payload,
|
||||
}),
|
||||
),
|
||||
retry: 0,
|
||||
})
|
||||
}
|
||||
@@ -945,15 +958,14 @@ export const useInstallOrUpdate = ({
|
||||
// From local bundle don't have data.value.github_plugin_unique_identifier
|
||||
uniqueIdentifier = data.value.github_plugin_unique_identifier!
|
||||
if (!uniqueIdentifier) {
|
||||
const { unique_identifier } = await post<uploadGitHubResponse>(
|
||||
'/workspaces/current/plugin/upload/github',
|
||||
{
|
||||
const { unique_identifier } = await narrowResponse<uploadGitHubResponse>(
|
||||
consoleClient.workspaces.current.plugin.upload.github.post({
|
||||
body: {
|
||||
repo: data.value.repo!,
|
||||
version: data.value.release! || data.value.version!,
|
||||
package: data.value.packages! || data.value.package!,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
uniqueIdentifier = data.value.github_plugin_unique_identifier! || unique_identifier
|
||||
// has the same version, but not installed
|
||||
@@ -966,16 +978,15 @@ export const useInstallOrUpdate = ({
|
||||
}
|
||||
}
|
||||
if (!isInstalled) {
|
||||
const response = await post<InstallPackageResponse>(
|
||||
'/workspaces/current/plugin/install/github',
|
||||
{
|
||||
const response = await narrowResponse<InstallPackageResponse>(
|
||||
consoleClient.workspaces.current.plugin.install.github.post({
|
||||
body: {
|
||||
repo: data.value.repo!,
|
||||
version: data.value.release! || data.value.version!,
|
||||
package: data.value.packages! || data.value.package!,
|
||||
plugin_unique_identifier: uniqueIdentifier,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
upsertStartedPluginTask(queryClient, response)
|
||||
const { task_id, all_installed } = response
|
||||
@@ -997,13 +1008,12 @@ export const useInstallOrUpdate = ({
|
||||
}
|
||||
}
|
||||
if (!isInstalled) {
|
||||
const response = await post<InstallPackageResponse>(
|
||||
'/workspaces/current/plugin/install/marketplace',
|
||||
{
|
||||
const response = await narrowResponse<InstallPackageResponse>(
|
||||
consoleClient.workspaces.current.plugin.install.marketplace.post({
|
||||
body: {
|
||||
plugin_unique_identifiers: [uniqueIdentifier],
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
upsertStartedPluginTask(queryClient, response)
|
||||
const { task_id, all_installed } = response
|
||||
@@ -1022,13 +1032,12 @@ export const useInstallOrUpdate = ({
|
||||
}
|
||||
}
|
||||
if (!isInstalled) {
|
||||
const response = await post<InstallPackageResponse>(
|
||||
'/workspaces/current/plugin/install/pkg',
|
||||
{
|
||||
const response = await narrowResponse<InstallPackageResponse>(
|
||||
consoleClient.workspaces.current.plugin.install.pkg.post({
|
||||
body: {
|
||||
plugin_unique_identifiers: [uniqueIdentifier],
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
upsertStartedPluginTask(queryClient, response)
|
||||
const { task_id, all_installed } = response
|
||||
@@ -1039,13 +1048,12 @@ export const useInstallOrUpdate = ({
|
||||
if (isInstalled) {
|
||||
if (item.type === 'package') {
|
||||
await uninstallPlugin(installedPayload.installedId, { preserveCredentials: true })
|
||||
const response = await post<InstallPackageResponse>(
|
||||
'/workspaces/current/plugin/install/pkg',
|
||||
{
|
||||
const response = await narrowResponse<InstallPackageResponse>(
|
||||
consoleClient.workspaces.current.plugin.install.pkg.post({
|
||||
body: {
|
||||
plugin_unique_identifiers: [uniqueIdentifier],
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
upsertStartedPluginTask(queryClient, response)
|
||||
const { task_id, all_installed } = response
|
||||
@@ -1087,7 +1095,7 @@ export const useInstallOrUpdate = ({
|
||||
export const useDebugKey = () => {
|
||||
return useQuery({
|
||||
queryKey: [NAME_SPACE, 'debugKey'],
|
||||
queryFn: () => get<DebugInfoTypes>('/workspaces/current/plugin/debugging-key'),
|
||||
queryFn: () => consoleClient.workspaces.current.plugin.debuggingKey.get(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1151,7 +1159,7 @@ const areAutoUpgradeSettingsEqual = (
|
||||
export const usePluginPermissionSettings = () => {
|
||||
return useQuery({
|
||||
queryKey: usePluginPermissionSettingsKey,
|
||||
queryFn: () => get<Permissions>('/workspaces/current/plugin/permission/fetch'),
|
||||
queryFn: () => consoleClient.workspaces.current.plugin.permission.fetch.get(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1159,9 +1167,7 @@ export const usePluginAutoUpgradeSettings = (category: PluginCategoryEnum) => {
|
||||
return useQuery({
|
||||
queryKey: pluginAutoUpgradeSettingsQueryKey(category),
|
||||
queryFn: () =>
|
||||
get<PluginAutoUpgradeSettingsResponse>('/workspaces/current/plugin/auto-upgrade/fetch', {
|
||||
params: { category },
|
||||
}),
|
||||
consoleClient.workspaces.current.plugin.autoUpgrade.fetch.get({ query: { category } }),
|
||||
staleTime: 60 * 1000,
|
||||
})
|
||||
}
|
||||
@@ -1184,7 +1190,7 @@ export const useMutationPluginPermissionSettings = ({
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (payload: Permissions) => {
|
||||
return post('/workspaces/current/plugin/permission/change', { body: payload })
|
||||
return consoleClient.workspaces.current.plugin.permission.change.post({ body: payload })
|
||||
},
|
||||
onMutate: async (payload) => {
|
||||
await queryClient.cancelQueries({ queryKey: usePluginPermissionSettingsKey })
|
||||
@@ -1219,7 +1225,7 @@ export const useMutationPluginAutoUpgradeSettings = ({
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (payload: AutoUpdateConfig) => {
|
||||
return post('/workspaces/current/plugin/auto-upgrade/change', {
|
||||
return consoleClient.workspaces.current.plugin.autoUpgrade.change.post({
|
||||
body: {
|
||||
category,
|
||||
auto_upgrade: payload,
|
||||
@@ -1270,14 +1276,16 @@ export const useMutationReferenceSettings = ({
|
||||
|
||||
if (!arePermissionsEqual(payload.permission, currentReferenceSetting?.permission))
|
||||
mutations.push(
|
||||
post('/workspaces/current/plugin/permission/change', { body: payload.permission }),
|
||||
consoleClient.workspaces.current.plugin.permission.change.post({
|
||||
body: payload.permission,
|
||||
}),
|
||||
)
|
||||
|
||||
if (
|
||||
!areAutoUpgradeSettingsEqual(payload.auto_upgrade, currentReferenceSetting?.auto_upgrade)
|
||||
) {
|
||||
mutations.push(
|
||||
post('/workspaces/current/plugin/auto-upgrade/change', {
|
||||
consoleClient.workspaces.current.plugin.autoUpgrade.change.post({
|
||||
body: {
|
||||
category,
|
||||
auto_upgrade: payload.auto_upgrade,
|
||||
@@ -1358,7 +1366,7 @@ export const useRemoveAutoUpgrade = () => {
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (payload: { plugin_id: string; category: PluginCategoryEnum }) => {
|
||||
return post('/workspaces/current/plugin/auto-upgrade/exclude', { body: payload })
|
||||
return consoleClient.workspaces.current.plugin.autoUpgrade.exclude.post({ body: payload })
|
||||
},
|
||||
onSuccess: (_data, payload) => {
|
||||
queryClient.invalidateQueries({
|
||||
@@ -1414,7 +1422,9 @@ export const usePluginTaskList = (category?: PluginCategoryEnum | string) => {
|
||||
enabled: canManagement,
|
||||
queryKey: usePluginTaskListKey,
|
||||
queryFn: () =>
|
||||
get<{ tasks: PluginTask[] }>('/workspaces/current/plugin/tasks?page=1&page_size=100'),
|
||||
narrowResponse<{ tasks: PluginTask[] }>(
|
||||
consoleClient.workspaces.current.plugin.tasks.get({ query: { page: 1, page_size: 100 } }),
|
||||
),
|
||||
structuralSharing: (previousData, nextData) =>
|
||||
preserveLocalUnfinishedPluginTasks(
|
||||
previousData as PluginTaskListResponse | undefined,
|
||||
@@ -1478,10 +1488,9 @@ export const usePluginTaskList = (category?: PluginCategoryEnum | string) => {
|
||||
export const useMutationClearTaskPlugin = () => {
|
||||
return useMutation({
|
||||
mutationFn: ({ taskId, pluginId }: { taskId: string; pluginId: string }) => {
|
||||
const encodedPluginId = encodeURIComponent(pluginId)
|
||||
return post<{ success: boolean }>(
|
||||
`/workspaces/current/plugin/tasks/${taskId}/delete/${encodedPluginId}`,
|
||||
)
|
||||
return consoleClient.workspaces.current.plugin.tasks.byTaskId.delete.byIdentifier.post({
|
||||
params: { task_id: taskId, identifier: pluginId },
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -1503,20 +1512,23 @@ export const useFetchDynamicOptions = (
|
||||
provider: string,
|
||||
action: string,
|
||||
parameter: string,
|
||||
provider_type?: 'tool' | 'trigger',
|
||||
// The backend requires a provider type; callers without one render tool options.
|
||||
provider_type: 'tool' | 'trigger' = 'tool',
|
||||
extra?: Record<string, unknown>,
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () =>
|
||||
get<{ options: FormOption[] }>('/workspaces/current/plugin/parameters/dynamic-options', {
|
||||
params: {
|
||||
plugin_id,
|
||||
provider,
|
||||
action,
|
||||
parameter,
|
||||
provider_type,
|
||||
...extra,
|
||||
},
|
||||
}),
|
||||
narrowResponse<{ options: FormOption[] }>(
|
||||
consoleClient.workspaces.current.plugin.parameters.dynamicOptions.get({
|
||||
query: {
|
||||
plugin_id,
|
||||
provider,
|
||||
action,
|
||||
parameter,
|
||||
provider_type,
|
||||
...extra,
|
||||
},
|
||||
}),
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user