diff --git a/web/service/workflow-payload.ts b/web/service/workflow-payload.ts new file mode 100644 index 0000000000..f0db7e8397 --- /dev/null +++ b/web/service/workflow-payload.ts @@ -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 +} + +export type WorkflowDraftSyncParams = Pick< + FetchWorkflowDraftResponse, + 'graph' | 'features' | 'environment_variables' | 'conversation_variables' +> + +const sanitizeTriggerPluginNode = (node: Node): Node => { + 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 + : {}, + } + + 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)) + + return { + ...params, + graph: { + ...graph, + nodes: sanitizedNodes, + }, + } +} diff --git a/web/service/workflow.ts b/web/service/workflow.ts index 654fe3d01a..19279f2a03 100644 --- a/web/service/workflow.ts +++ b/web/service/workflow.ts @@ -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 @@ -19,9 +21,10 @@ export const fetchWorkflowDraft = (url: string) => { export const syncWorkflowDraft = ({ url, params }: { url: string - params: Pick + params: WorkflowDraftSyncParams }) => { - return post(url, { body: params }, { silent: true }) + const sanitizedParams = sanitizeWorkflowDraftPayload(params) + return post(url, { body: sanitizedParams }, { silent: true }) } export const fetchNodesDefaultConfigs: Fetcher = (url) => {