feat: last run frontend (#21369)
The frontend of feat: Persist Variables for Enhanced Debugging Workflow (#20699). Co-authored-by: jZonG <jzongcode@gmail.com>
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
import { del, get, patch, post } from './base'
|
||||
import { del, get, patch, post, put } from './base'
|
||||
import { useInfiniteQuery, useMutation, useQuery } from '@tanstack/react-query'
|
||||
import type {
|
||||
FetchWorkflowDraftPageParams,
|
||||
FetchWorkflowDraftPageResponse,
|
||||
FetchWorkflowDraftResponse,
|
||||
NodeTracing,
|
||||
PublishWorkflowParams,
|
||||
UpdateWorkflowParams,
|
||||
VarInInspect,
|
||||
WorkflowConfigResponse,
|
||||
} from '@/types/workflow'
|
||||
import type { CommonResponse } from '@/models/common'
|
||||
import { useReset } from './use-base'
|
||||
import { useInvalid, useReset } from './use-base'
|
||||
|
||||
const NAME_SPACE = 'workflow'
|
||||
|
||||
@@ -85,3 +87,118 @@ export const usePublishWorkflow = (appId: string) => {
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
const useLastRunKey = [NAME_SPACE, 'last-run']
|
||||
export const useLastRun = (appID: string, nodeId: string, enabled: boolean) => {
|
||||
return useQuery<NodeTracing>({
|
||||
enabled,
|
||||
queryKey: [...useLastRunKey, appID, nodeId],
|
||||
queryFn: async () => {
|
||||
return get(`apps/${appID}/workflows/draft/nodes/${nodeId}/last-run`, {}, {
|
||||
silent: true,
|
||||
})
|
||||
},
|
||||
retry: 0,
|
||||
})
|
||||
}
|
||||
|
||||
export const useInvalidLastRun = (appId: string, nodeId: string) => {
|
||||
return useInvalid([NAME_SPACE, 'last-run', appId, nodeId])
|
||||
}
|
||||
|
||||
// Rerun workflow or change the version of workflow
|
||||
export const useInvalidAllLastRun = (appId: string) => {
|
||||
return useInvalid([NAME_SPACE, 'last-run', appId])
|
||||
}
|
||||
|
||||
const useConversationVarValuesKey = [NAME_SPACE, 'conversation-variable']
|
||||
|
||||
export const useConversationVarValues = (appId: string) => {
|
||||
return useQuery({
|
||||
queryKey: [...useConversationVarValuesKey, appId],
|
||||
queryFn: async () => {
|
||||
const { items } = (await get(`apps/${appId}/workflows/draft/conversation-variables`)) as { items: VarInInspect[] }
|
||||
return items
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const useInvalidateConversationVarValues = (appId: string) => {
|
||||
return useInvalid([...useConversationVarValuesKey, appId])
|
||||
}
|
||||
|
||||
export const useResetConversationVar = (appId: string) => {
|
||||
return useMutation({
|
||||
mutationKey: [NAME_SPACE, 'reset conversation var', appId],
|
||||
mutationFn: async (varId: string) => {
|
||||
return put(`apps/${appId}/workflows/draft/variables/${varId}/reset`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const useResetToLastRunValue = (appId: string) => {
|
||||
return useMutation({
|
||||
mutationKey: [NAME_SPACE, 'reset to last run value', appId],
|
||||
mutationFn: async (varId: string) => {
|
||||
return put(`apps/${appId}/workflows/draft/variables/${varId}/reset`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const useSysVarValuesKey = [NAME_SPACE, 'sys-variable']
|
||||
export const useSysVarValues = (appId: string) => {
|
||||
return useQuery({
|
||||
queryKey: [...useSysVarValuesKey, appId],
|
||||
queryFn: async () => {
|
||||
const { items } = (await get(`apps/${appId}/workflows/draft/system-variables`)) as { items: VarInInspect[] }
|
||||
return items
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const useInvalidateSysVarValues = (appId: string) => {
|
||||
return useInvalid([...useSysVarValuesKey, appId])
|
||||
}
|
||||
|
||||
export const useDeleteAllInspectorVars = (appId: string) => {
|
||||
return useMutation({
|
||||
mutationKey: [NAME_SPACE, 'delete all inspector vars', appId],
|
||||
mutationFn: async () => {
|
||||
return del(`apps/${appId}/workflows/draft/variables`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteNodeInspectorVars = (appId: string) => {
|
||||
return useMutation({
|
||||
mutationKey: [NAME_SPACE, 'delete node inspector vars', appId],
|
||||
mutationFn: async (nodeId: string) => {
|
||||
return del(`apps/${appId}/workflows/draft/nodes/${nodeId}/variables`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteInspectVar = (appId: string) => {
|
||||
return useMutation({
|
||||
mutationKey: [NAME_SPACE, 'delete inspector var', appId],
|
||||
mutationFn: async (varId: string) => {
|
||||
return del(`apps/${appId}/workflows/draft/variables/${varId}`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// edit the name or value of the inspector var
|
||||
export const useEditInspectorVar = (appId: string) => {
|
||||
return useMutation({
|
||||
mutationKey: [NAME_SPACE, 'edit inspector var', appId],
|
||||
mutationFn: async ({ varId, ...rest }: {
|
||||
varId: string
|
||||
name?: string
|
||||
value?: any
|
||||
}) => {
|
||||
return patch(`apps/${appId}/workflows/draft/variables/${varId}`, {
|
||||
body: rest,
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
|
@@ -9,6 +9,7 @@ import type {
|
||||
WorkflowRunHistoryResponse,
|
||||
} from '@/types/workflow'
|
||||
import type { BlockEnum } from '@/app/components/workflow/types'
|
||||
import type { VarInInspect } from '@/types/workflow'
|
||||
|
||||
export const fetchWorkflowDraft = (url: string) => {
|
||||
return get(url, {}, { silent: true }) as Promise<FetchWorkflowDraftResponse>
|
||||
@@ -70,3 +71,31 @@ export const fetchCurrentValueOfConversationVariable: Fetcher<ConversationVariab
|
||||
}> = ({ url, params }) => {
|
||||
return get<ConversationVariableResponse>(url, { params })
|
||||
}
|
||||
|
||||
const fetchAllInspectVarsOnePage = async (appId: string, page: number): Promise<{ total: number, items: VarInInspect[] }> => {
|
||||
return get(`apps/${appId}/workflows/draft/variables`, {
|
||||
params: { page, limit: 100 },
|
||||
})
|
||||
}
|
||||
export const fetchAllInspectVars = async (appId: string): Promise<VarInInspect[]> => {
|
||||
const res = await fetchAllInspectVarsOnePage(appId, 1)
|
||||
const { items, total } = res
|
||||
if (total <= 100)
|
||||
return items
|
||||
|
||||
const pageCount = Math.ceil(total / 100)
|
||||
const promises = []
|
||||
for (let i = 2; i <= pageCount; i++)
|
||||
promises.push(fetchAllInspectVarsOnePage(appId, i))
|
||||
|
||||
const restData = await Promise.all(promises)
|
||||
restData.forEach(({ items: item }) => {
|
||||
items.push(...item)
|
||||
})
|
||||
return items
|
||||
}
|
||||
|
||||
export const fetchNodeInspectVars = async (appId: string, nodeId: string): Promise<VarInInspect[]> => {
|
||||
const { items } = (await get(`apps/${appId}/workflows/draft/nodes/${nodeId}/variables`)) as { items: VarInInspect[] }
|
||||
return items
|
||||
}
|
||||
|
Reference in New Issue
Block a user