feat(trigger): add trigger validation logic and utility functions for improved checklist integration

This commit is contained in:
zhsama 2025-10-20 20:26:40 +08:00
parent f5c1646f79
commit d4b5d9a02a
3 changed files with 101 additions and 1 deletions

View File

@ -22,6 +22,7 @@ import {
getToolCheckParams,
getValidTreeNodes,
} from '../utils'
import { getTriggerCheckParams } from '../utils/trigger'
import {
CUSTOM_NODE,
} from '../constants'
@ -31,10 +32,12 @@ import {
} from '../hooks'
import type { ToolNodeType } from '../nodes/tool/types'
import type { DataSourceNodeType } from '../nodes/data-source/types'
import type { PluginTriggerNodeType } from '../nodes/trigger-plugin/types'
import { useToastContext } from '@/app/components/base/toast'
import { useGetLanguage } from '@/context/i18n'
import type { AgentNodeType } from '../nodes/agent/types'
import { useStrategyProviders } from '@/service/use-strategy'
import { useAllTriggerPlugins } from '@/service/use-triggers'
import { useDatasetsDetailStore } from '../datasets-detail-store/store'
import type { KnowledgeRetrievalNodeType } from '../nodes/knowledge-retrieval/types'
import type { DataSet } from '@/models/datasets'
@ -70,6 +73,7 @@ export const useChecklist = (nodes: Node[], edges: Edge[]) => {
const workflowTools = useStore(s => s.workflowTools)
const dataSourceList = useStore(s => s.dataSourceList)
const { data: strategyProviders } = useStrategyProviders()
const { data: triggerPlugins } = useAllTriggerPlugins()
const datasetsDetail = useDatasetsDetailStore(s => s.datasetsDetail)
const getToolIcon = useGetToolIcon()
@ -108,6 +112,9 @@ export const useChecklist = (nodes: Node[], edges: Edge[]) => {
if (node.data.type === BlockEnum.DataSource)
moreDataForCheckValid = getDataSourceCheckParams(node.data as DataSourceNodeType, dataSourceList || [], language)
if (node.data.type === BlockEnum.TriggerPlugin)
moreDataForCheckValid = getTriggerCheckParams(node.data as PluginTriggerNodeType, triggerPlugins, language)
const toolIcon = getToolIcon(node.data)
if (node.data.type === BlockEnum.Agent) {
const data = node.data as AgentNodeType

View File

@ -5,6 +5,7 @@ import type { PluginTriggerNodeType } from './types'
import { getMatchedSchemaType } from '../_base/components/variable/use-match-schema-type'
import type { SchemaTypeDefinition } from '@/service/use-common'
import { type Field, type StructuredOutput, Type } from '../llm/types'
import { VarKindType } from '../_base/types'
const normalizeJsonSchemaType = (schema: any): string | undefined => {
if (!schema) return undefined
@ -222,12 +223,52 @@ const nodeDefault: NodeDefault<PluginTriggerNodeType> = {
// event_type: '',
config: {},
},
checkValid(payload: PluginTriggerNodeType, t: any) {
checkValid(payload: PluginTriggerNodeType, t: any, moreDataForCheckValid: {
triggerInputsSchema?: Array<{
variable: string
label: string
required?: boolean
}>
isReadyForCheckValid?: boolean
} = {}) {
let errorMessage = ''
if (!payload.subscription_id)
errorMessage = t('workflow.nodes.triggerPlugin.subscriptionRequired')
const {
triggerInputsSchema = [],
isReadyForCheckValid = true,
} = moreDataForCheckValid || {}
if (!errorMessage && isReadyForCheckValid) {
triggerInputsSchema.filter(field => field.required).forEach((field) => {
if (errorMessage)
return
const rawParam = payload.event_parameters?.[field.variable]
?? (payload.config as Record<string, any> | undefined)?.[field.variable]
if (!rawParam) {
errorMessage = t('workflow.errorMsg.fieldRequired', { field: field.label })
return
}
const targetParam = typeof rawParam === 'object' && rawParam !== null && 'type' in rawParam
? rawParam as { type: VarKindType; value: any }
: { type: VarKindType.constant, value: rawParam }
const { type, value } = targetParam
if (type === VarKindType.variable) {
if (!value || (Array.isArray(value) && value.length === 0))
errorMessage = t('workflow.errorMsg.fieldRequired', { field: field.label })
}
else {
if (value === undefined || value === null || value === '')
errorMessage = t('workflow.errorMsg.fieldRequired', { field: field.label })
}
})
}
return {
isValid: !errorMessage,
errorMessage,

View File

@ -0,0 +1,52 @@
import type { TriggerWithProvider } from '@/app/components/workflow/block-selector/types'
import type { PluginTriggerNodeType } from '@/app/components/workflow/nodes/trigger-plugin/types'
export type TriggerCheckParams = {
triggerInputsSchema: Array<{
variable: string
label: string
required?: boolean
}>
isReadyForCheckValid: boolean
}
export const getTriggerCheckParams = (
triggerData: PluginTriggerNodeType,
triggerProviders: TriggerWithProvider[] | undefined,
language: string,
): TriggerCheckParams => {
if (!triggerProviders) {
return {
triggerInputsSchema: [],
isReadyForCheckValid: false,
}
}
const {
provider_id,
provider_name,
event_name,
} = triggerData
const provider = triggerProviders.find(item =>
item.name === provider_name
|| item.id === provider_id
|| (provider_id && item.plugin_id === provider_id),
)
const currentEvent = provider?.events.find(event => event.name === event_name)
const triggerInputsSchema = (currentEvent?.parameters || []).map((parameter) => {
const label = parameter.label?.[language] || parameter.label?.en_US || parameter.name
return {
variable: parameter.name,
label,
required: parameter.required,
}
})
return {
triggerInputsSchema,
isReadyForCheckValid: true,
}
}