mirror of
https://github.com/langgenius/dify.git
synced 2026-05-08 03:36:32 +08:00
91 lines
2.4 KiB
TypeScript
91 lines
2.4 KiB
TypeScript
import type { EvaluationResourceType } from '@/app/components/evaluation/types'
|
|
import type { AvailableEvaluationWorkflowsResponse, EvaluationConfig } from '@/types/evaluation'
|
|
import {
|
|
keepPreviousData,
|
|
useInfiniteQuery,
|
|
useMutation,
|
|
useQuery,
|
|
} from '@tanstack/react-query'
|
|
import { consoleClient, consoleQuery } from '@/service/client'
|
|
|
|
type AvailableEvaluationWorkflowsParams = {
|
|
page?: number
|
|
limit?: number
|
|
keyword?: string
|
|
userId?: string
|
|
}
|
|
|
|
const normalizeAvailableEvaluationWorkflowsParams = (params: AvailableEvaluationWorkflowsParams = {}) => {
|
|
const page = params.page ?? 1
|
|
const limit = params.limit ?? 20
|
|
const keyword = params.keyword?.trim()
|
|
const userId = params.userId?.trim()
|
|
|
|
return {
|
|
page,
|
|
limit,
|
|
...(keyword ? { keyword } : {}),
|
|
...(userId ? { user_id: userId } : {}),
|
|
}
|
|
}
|
|
|
|
const getEvaluationConfigQueryOptions = (
|
|
resourceType: EvaluationResourceType,
|
|
resourceId: string,
|
|
) => {
|
|
return consoleQuery.evaluation.config.queryOptions({
|
|
input: {
|
|
params: {
|
|
targetType: resourceType,
|
|
targetId: resourceId,
|
|
},
|
|
},
|
|
enabled: !!resourceId,
|
|
refetchOnWindowFocus: false,
|
|
})
|
|
}
|
|
|
|
export const useEvaluationConfig = (
|
|
resourceType: EvaluationResourceType,
|
|
resourceId: string,
|
|
) => {
|
|
return useQuery<EvaluationConfig>(getEvaluationConfigQueryOptions(resourceType, resourceId))
|
|
}
|
|
|
|
export const useAvailableEvaluationMetrics = (enabled = true) => {
|
|
return useQuery(consoleQuery.evaluation.availableMetrics.queryOptions({
|
|
enabled,
|
|
}))
|
|
}
|
|
|
|
export const useEvaluationNodeInfoMutation = () => {
|
|
return useMutation(consoleQuery.evaluation.nodeInfo.mutationOptions())
|
|
}
|
|
|
|
export const useAvailableEvaluationWorkflows = (
|
|
params: AvailableEvaluationWorkflowsParams = {},
|
|
options?: { enabled?: boolean },
|
|
) => {
|
|
const queryParams = normalizeAvailableEvaluationWorkflowsParams(params)
|
|
|
|
return useInfiniteQuery<AvailableEvaluationWorkflowsResponse>({
|
|
queryKey: consoleQuery.evaluation.availableWorkflows.queryKey({
|
|
input: {
|
|
query: queryParams,
|
|
},
|
|
}),
|
|
queryFn: ({ pageParam = queryParams.page }) => {
|
|
return consoleClient.evaluation.availableWorkflows({
|
|
query: {
|
|
...queryParams,
|
|
page: Number(pageParam),
|
|
},
|
|
})
|
|
},
|
|
getNextPageParam: lastPage => lastPage.has_more ? lastPage.page + 1 : undefined,
|
|
initialPageParam: queryParams.page,
|
|
placeholderData: keepPreviousData,
|
|
...options,
|
|
})
|
|
}
|