feat(workflow): integrate payload sanitization for workflow draft synchronization

This commit is contained in:
zhsama 2025-10-23 16:45:28 +08:00
parent 42a9a88ae2
commit f822b38a00
2 changed files with 67 additions and 2 deletions

View File

@ -0,0 +1,62 @@
import { BlockEnum } from '@/app/components/workflow/types'
import type { Node } from '@/app/components/workflow/types'
import type { FetchWorkflowDraftResponse } from '@/types/workflow'
export type TriggerPluginNodePayload = {
title: string
desc?: string
plugin_id: string
provider_id: string
event_name: string
subscription_id: string
plugin_unique_identifier: string
event_parameters: Record<string, unknown>
}
export type WorkflowDraftSyncParams = Pick<
FetchWorkflowDraftResponse,
'graph' | 'features' | 'environment_variables' | 'conversation_variables'
>
const sanitizeTriggerPluginNode = (node: Node<TriggerPluginNodePayload>): Node<TriggerPluginNodePayload> => {
const data = node.data
if (!data || data.type !== BlockEnum.TriggerPlugin)
return node
const sanitizedData: TriggerPluginNodePayload & { type: BlockEnum.TriggerPlugin } = {
type: BlockEnum.TriggerPlugin,
title: data.title ?? '',
desc: data.desc,
plugin_id: data.plugin_id ?? '',
provider_id: data.provider_id ?? '',
event_name: data.event_name ?? '',
subscription_id: data.subscription_id ?? '',
plugin_unique_identifier: data.plugin_unique_identifier ?? '',
event_parameters: (typeof data.event_parameters === 'object' && data.event_parameters !== null)
? data.event_parameters as Record<string, unknown>
: {},
}
return {
...node,
data: sanitizedData,
}
}
export const sanitizeWorkflowDraftPayload = (params: WorkflowDraftSyncParams): WorkflowDraftSyncParams => {
const { graph } = params
if (!graph?.nodes?.length)
return params
const sanitizedNodes = graph.nodes.map(node => sanitizeTriggerPluginNode(node as Node<TriggerPluginNodePayload>))
return {
...params,
graph: {
...graph,
nodes: sanitizedNodes,
},
}
}

View File

@ -12,6 +12,8 @@ import type { BlockEnum } from '@/app/components/workflow/types'
import type { VarInInspect } from '@/types/workflow'
import type { FlowType } from '@/types/common'
import { getFlowPrefix } from './utils'
import { sanitizeWorkflowDraftPayload } from './workflow-payload'
import type { WorkflowDraftSyncParams } from './workflow-payload'
export const fetchWorkflowDraft = (url: string) => {
return get(url, {}, { silent: true }) as Promise<FetchWorkflowDraftResponse>
@ -19,9 +21,10 @@ export const fetchWorkflowDraft = (url: string) => {
export const syncWorkflowDraft = ({ url, params }: {
url: string
params: Pick<FetchWorkflowDraftResponse, 'graph' | 'features' | 'environment_variables' | 'conversation_variables'>
params: WorkflowDraftSyncParams
}) => {
return post<CommonResponse & { updated_at: number; hash: string }>(url, { body: params }, { silent: true })
const sanitizedParams = sanitizeWorkflowDraftPayload(params)
return post<CommonResponse & { updated_at: number; hash: string }>(url, { body: sanitizedParams }, { silent: true })
}
export const fetchNodesDefaultConfigs: Fetcher<NodesDefaultConfigsResponse, string> = (url) => {