mirror of
https://github.com/langgenius/dify.git
synced 2026-05-01 22:47:15 +08:00
feat(trigger): add trigger validation logic and utility functions for improved checklist integration
This commit is contained in:
parent
f5c1646f79
commit
d4b5d9a02a
@ -22,6 +22,7 @@ import {
|
|||||||
getToolCheckParams,
|
getToolCheckParams,
|
||||||
getValidTreeNodes,
|
getValidTreeNodes,
|
||||||
} from '../utils'
|
} from '../utils'
|
||||||
|
import { getTriggerCheckParams } from '../utils/trigger'
|
||||||
import {
|
import {
|
||||||
CUSTOM_NODE,
|
CUSTOM_NODE,
|
||||||
} from '../constants'
|
} from '../constants'
|
||||||
@ -31,10 +32,12 @@ import {
|
|||||||
} from '../hooks'
|
} from '../hooks'
|
||||||
import type { ToolNodeType } from '../nodes/tool/types'
|
import type { ToolNodeType } from '../nodes/tool/types'
|
||||||
import type { DataSourceNodeType } from '../nodes/data-source/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 { useToastContext } from '@/app/components/base/toast'
|
||||||
import { useGetLanguage } from '@/context/i18n'
|
import { useGetLanguage } from '@/context/i18n'
|
||||||
import type { AgentNodeType } from '../nodes/agent/types'
|
import type { AgentNodeType } from '../nodes/agent/types'
|
||||||
import { useStrategyProviders } from '@/service/use-strategy'
|
import { useStrategyProviders } from '@/service/use-strategy'
|
||||||
|
import { useAllTriggerPlugins } from '@/service/use-triggers'
|
||||||
import { useDatasetsDetailStore } from '../datasets-detail-store/store'
|
import { useDatasetsDetailStore } from '../datasets-detail-store/store'
|
||||||
import type { KnowledgeRetrievalNodeType } from '../nodes/knowledge-retrieval/types'
|
import type { KnowledgeRetrievalNodeType } from '../nodes/knowledge-retrieval/types'
|
||||||
import type { DataSet } from '@/models/datasets'
|
import type { DataSet } from '@/models/datasets'
|
||||||
@ -70,6 +73,7 @@ export const useChecklist = (nodes: Node[], edges: Edge[]) => {
|
|||||||
const workflowTools = useStore(s => s.workflowTools)
|
const workflowTools = useStore(s => s.workflowTools)
|
||||||
const dataSourceList = useStore(s => s.dataSourceList)
|
const dataSourceList = useStore(s => s.dataSourceList)
|
||||||
const { data: strategyProviders } = useStrategyProviders()
|
const { data: strategyProviders } = useStrategyProviders()
|
||||||
|
const { data: triggerPlugins } = useAllTriggerPlugins()
|
||||||
const datasetsDetail = useDatasetsDetailStore(s => s.datasetsDetail)
|
const datasetsDetail = useDatasetsDetailStore(s => s.datasetsDetail)
|
||||||
const getToolIcon = useGetToolIcon()
|
const getToolIcon = useGetToolIcon()
|
||||||
|
|
||||||
@ -108,6 +112,9 @@ export const useChecklist = (nodes: Node[], edges: Edge[]) => {
|
|||||||
if (node.data.type === BlockEnum.DataSource)
|
if (node.data.type === BlockEnum.DataSource)
|
||||||
moreDataForCheckValid = getDataSourceCheckParams(node.data as DataSourceNodeType, dataSourceList || [], language)
|
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)
|
const toolIcon = getToolIcon(node.data)
|
||||||
if (node.data.type === BlockEnum.Agent) {
|
if (node.data.type === BlockEnum.Agent) {
|
||||||
const data = node.data as AgentNodeType
|
const data = node.data as AgentNodeType
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import type { PluginTriggerNodeType } from './types'
|
|||||||
import { getMatchedSchemaType } from '../_base/components/variable/use-match-schema-type'
|
import { getMatchedSchemaType } from '../_base/components/variable/use-match-schema-type'
|
||||||
import type { SchemaTypeDefinition } from '@/service/use-common'
|
import type { SchemaTypeDefinition } from '@/service/use-common'
|
||||||
import { type Field, type StructuredOutput, Type } from '../llm/types'
|
import { type Field, type StructuredOutput, Type } from '../llm/types'
|
||||||
|
import { VarKindType } from '../_base/types'
|
||||||
|
|
||||||
const normalizeJsonSchemaType = (schema: any): string | undefined => {
|
const normalizeJsonSchemaType = (schema: any): string | undefined => {
|
||||||
if (!schema) return undefined
|
if (!schema) return undefined
|
||||||
@ -222,12 +223,52 @@ const nodeDefault: NodeDefault<PluginTriggerNodeType> = {
|
|||||||
// event_type: '',
|
// event_type: '',
|
||||||
config: {},
|
config: {},
|
||||||
},
|
},
|
||||||
checkValid(payload: PluginTriggerNodeType, t: any) {
|
checkValid(payload: PluginTriggerNodeType, t: any, moreDataForCheckValid: {
|
||||||
|
triggerInputsSchema?: Array<{
|
||||||
|
variable: string
|
||||||
|
label: string
|
||||||
|
required?: boolean
|
||||||
|
}>
|
||||||
|
isReadyForCheckValid?: boolean
|
||||||
|
} = {}) {
|
||||||
let errorMessage = ''
|
let errorMessage = ''
|
||||||
|
|
||||||
if (!payload.subscription_id)
|
if (!payload.subscription_id)
|
||||||
errorMessage = t('workflow.nodes.triggerPlugin.subscriptionRequired')
|
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 {
|
return {
|
||||||
isValid: !errorMessage,
|
isValid: !errorMessage,
|
||||||
errorMessage,
|
errorMessage,
|
||||||
|
|||||||
52
web/app/components/workflow/utils/trigger.ts
Normal file
52
web/app/components/workflow/utils/trigger.ts
Normal 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user