chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
+52
@@ -0,0 +1,52 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import type { CredentialFormSchema } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import type { Event } from '@/app/components/tools/types'
|
||||
import type { TriggerWithProvider } from '@/app/components/workflow/block-selector/types'
|
||||
import type { ResourceVarInputs } from '@/app/components/workflow/nodes/_base/types'
|
||||
import { TriggerFormItem } from './item'
|
||||
|
||||
type Props = Readonly<{
|
||||
readOnly: boolean
|
||||
nodeId: string
|
||||
schema: CredentialFormSchema[]
|
||||
value: ResourceVarInputs
|
||||
onChange: (value: ResourceVarInputs) => void
|
||||
onOpen?: (index: number) => void
|
||||
currentEvent?: Event
|
||||
currentProvider?: TriggerWithProvider
|
||||
extraParams?: Record<string, any>
|
||||
disableVariableInsertion?: boolean
|
||||
}>
|
||||
|
||||
const TriggerForm: FC<Props> = ({
|
||||
readOnly,
|
||||
nodeId,
|
||||
schema,
|
||||
value,
|
||||
onChange,
|
||||
currentEvent,
|
||||
currentProvider,
|
||||
extraParams,
|
||||
disableVariableInsertion = false,
|
||||
}) => {
|
||||
return (
|
||||
<div className="space-y-1">
|
||||
{schema.map((schema, index) => (
|
||||
<TriggerFormItem
|
||||
key={index}
|
||||
readOnly={readOnly}
|
||||
nodeId={nodeId}
|
||||
schema={schema}
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
currentEvent={currentEvent}
|
||||
currentProvider={currentProvider}
|
||||
extraParams={extraParams}
|
||||
disableVariableInsertion={disableVariableInsertion}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
export default TriggerForm
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
'use client'
|
||||
import type { CredentialFormSchema } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import type { Event } from '@/app/components/tools/types'
|
||||
import type { TriggerWithProvider } from '@/app/components/workflow/block-selector/types'
|
||||
import type { ResourceVarInputs } from '@/app/components/workflow/nodes/_base/types'
|
||||
import { Infotip, InfotipContent, InfotipTrigger } from '@langgenius/dify-ui/infotip'
|
||||
import { useId } from 'react'
|
||||
import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks'
|
||||
import { SchemaDialog } from '@/app/components/plugins/plugin-detail-panel/tool-selector/components/schema-dialog'
|
||||
import FormInputItem from '@/app/components/workflow/nodes/_base/components/form-input-item'
|
||||
|
||||
type Props = Readonly<{
|
||||
readOnly: boolean
|
||||
nodeId: string
|
||||
schema: CredentialFormSchema
|
||||
value: ResourceVarInputs
|
||||
onChange: (value: ResourceVarInputs) => void
|
||||
currentEvent?: Event
|
||||
currentProvider?: TriggerWithProvider
|
||||
extraParams?: Record<string, unknown>
|
||||
disableVariableInsertion?: boolean
|
||||
}>
|
||||
|
||||
export function TriggerFormItem({
|
||||
readOnly,
|
||||
nodeId,
|
||||
schema,
|
||||
value,
|
||||
onChange,
|
||||
currentEvent,
|
||||
currentProvider,
|
||||
extraParams,
|
||||
disableVariableInsertion = false,
|
||||
}: Props) {
|
||||
const language = useLanguage()
|
||||
const labelId = useId()
|
||||
const { name, label, type, required, tooltip, input_schema } = schema
|
||||
const showSchemaButton = type === FormTypeEnum.object || type === FormTypeEnum.array
|
||||
const showDescription =
|
||||
type === FormTypeEnum.textInput ||
|
||||
type === FormTypeEnum.textNumber ||
|
||||
type === FormTypeEnum.secretInput ||
|
||||
type === FormTypeEnum.date ||
|
||||
type === FormTypeEnum.dateRange
|
||||
return (
|
||||
<div className="space-y-0.5 py-1">
|
||||
<div>
|
||||
<div className="flex h-6 items-center">
|
||||
<div id={labelId} className="system-sm-medium text-text-secondary">
|
||||
{label[language] || label.en_US}
|
||||
</div>
|
||||
{required && (
|
||||
<div className="ml-1 system-xs-regular text-text-destructive-secondary">*</div>
|
||||
)}
|
||||
{!showDescription && tooltip && (
|
||||
<Infotip>
|
||||
<InfotipTrigger aria-labelledby={labelId} className="ml-1" />
|
||||
<InfotipContent aria-labelledby={labelId} className="w-50">
|
||||
{tooltip[language] || tooltip.en_US}
|
||||
</InfotipContent>
|
||||
</Infotip>
|
||||
)}
|
||||
{showSchemaButton && (
|
||||
<>
|
||||
<div className="mr-0.5 ml-1 system-xs-regular text-text-quaternary">·</div>
|
||||
<SchemaDialog schema={input_schema} rootName={name} />
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
{showDescription && tooltip && (
|
||||
<div className="pb-0.5 body-xs-regular text-text-tertiary">
|
||||
{tooltip[language] || tooltip.en_US}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<FormInputItem
|
||||
labelId={labelId}
|
||||
readOnly={readOnly}
|
||||
nodeId={nodeId}
|
||||
schema={schema}
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
currentTool={currentEvent}
|
||||
currentProvider={currentProvider}
|
||||
providerType="trigger"
|
||||
extraParams={extraParams}
|
||||
disableVariableInsertion={disableVariableInsertion}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
import type { SchemaDefinitionItemResponse } from '@dify/contracts/api/console/spec/types.gen'
|
||||
import type { TFunction } from 'i18next'
|
||||
import type { NodeDefault, Var } from '../../types'
|
||||
import type { Field, StructuredOutput } from '../llm/types'
|
||||
import type { PluginTriggerNodeType } from './types'
|
||||
import { BlockEnum, VarType } from '../../types'
|
||||
import { genNodeMetaData } from '../../utils'
|
||||
import { VarKindType } from '../_base/types'
|
||||
import { Type } from '../llm/types'
|
||||
|
||||
const normalizeJsonSchemaType = (schema: any): string | undefined => {
|
||||
if (!schema) return undefined
|
||||
const { type, properties, items, oneOf, anyOf, allOf } = schema
|
||||
|
||||
if (Array.isArray(type))
|
||||
return type.find((item: string | null) => item && item !== 'null') || type[0]
|
||||
|
||||
if (typeof type === 'string') return type
|
||||
|
||||
const compositeCandidates = [oneOf, anyOf, allOf]
|
||||
.filter((entry): entry is any[] => Array.isArray(entry))
|
||||
.flat()
|
||||
|
||||
for (const candidate of compositeCandidates) {
|
||||
const normalized = normalizeJsonSchemaType(candidate)
|
||||
if (normalized) return normalized
|
||||
}
|
||||
|
||||
if (properties) return 'object'
|
||||
|
||||
if (items) return 'array'
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
const pickItemSchema = (schema: any) => {
|
||||
if (!schema || !schema.items) return undefined
|
||||
return Array.isArray(schema.items) ? schema.items[0] : schema.items
|
||||
}
|
||||
|
||||
const extractSchemaType = (
|
||||
schema: any,
|
||||
_schemaTypeDefinitions?: SchemaDefinitionItemResponse[],
|
||||
): string | undefined => {
|
||||
if (!schema) return undefined
|
||||
|
||||
const schemaTypeFromSchema = schema.schema_type || schema.schemaType
|
||||
if (typeof schemaTypeFromSchema === 'string' && schemaTypeFromSchema.trim().length > 0)
|
||||
return schemaTypeFromSchema
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
const resolveVarType = (
|
||||
schema: any,
|
||||
schemaTypeDefinitions?: SchemaDefinitionItemResponse[],
|
||||
): { type: VarType; schemaType?: string } => {
|
||||
const schemaType = extractSchemaType(schema, schemaTypeDefinitions)
|
||||
const normalizedType = normalizeJsonSchemaType(schema)
|
||||
|
||||
switch (normalizedType) {
|
||||
case 'string':
|
||||
return { type: VarType.string, schemaType }
|
||||
case 'number':
|
||||
return { type: VarType.number, schemaType }
|
||||
case 'integer':
|
||||
return { type: VarType.integer, schemaType }
|
||||
case 'boolean':
|
||||
return { type: VarType.boolean, schemaType }
|
||||
case 'object':
|
||||
return { type: VarType.object, schemaType }
|
||||
case 'array': {
|
||||
const itemSchema = pickItemSchema(schema)
|
||||
if (!itemSchema) return { type: VarType.array, schemaType }
|
||||
|
||||
const { type: itemType, schemaType: itemSchemaType } = resolveVarType(
|
||||
itemSchema,
|
||||
schemaTypeDefinitions,
|
||||
)
|
||||
const resolvedSchemaType = schemaType || itemSchemaType
|
||||
|
||||
if (itemSchemaType === 'file')
|
||||
return { type: VarType.arrayFile, schemaType: resolvedSchemaType }
|
||||
|
||||
switch (itemType) {
|
||||
case VarType.string:
|
||||
return { type: VarType.arrayString, schemaType: resolvedSchemaType }
|
||||
case VarType.number:
|
||||
case VarType.integer:
|
||||
return { type: VarType.arrayNumber, schemaType: resolvedSchemaType }
|
||||
case VarType.boolean:
|
||||
return { type: VarType.arrayBoolean, schemaType: resolvedSchemaType }
|
||||
case VarType.object:
|
||||
return { type: VarType.arrayObject, schemaType: resolvedSchemaType }
|
||||
case VarType.file:
|
||||
return { type: VarType.arrayFile, schemaType: resolvedSchemaType }
|
||||
default:
|
||||
return { type: VarType.array, schemaType: resolvedSchemaType }
|
||||
}
|
||||
}
|
||||
default:
|
||||
return { type: VarType.any, schemaType }
|
||||
}
|
||||
}
|
||||
|
||||
const toFieldType = (normalizedType: string | undefined, schemaType?: string): Type => {
|
||||
if (schemaType === 'file') return normalizedType === 'array' ? Type.array : Type.file
|
||||
|
||||
switch (normalizedType) {
|
||||
case 'number':
|
||||
case 'integer':
|
||||
return Type.number
|
||||
case 'boolean':
|
||||
return Type.boolean
|
||||
case 'object':
|
||||
return Type.object
|
||||
case 'array':
|
||||
return Type.array
|
||||
case 'string':
|
||||
default:
|
||||
return Type.string
|
||||
}
|
||||
}
|
||||
|
||||
const toArrayItemType = (type: Type): Exclude<Type, typeof Type.array> => {
|
||||
if (type === Type.array) return Type.object
|
||||
return type
|
||||
}
|
||||
|
||||
const convertJsonSchemaToField = (
|
||||
schema: any,
|
||||
schemaTypeDefinitions?: SchemaDefinitionItemResponse[],
|
||||
): Field => {
|
||||
const schemaType = extractSchemaType(schema, schemaTypeDefinitions)
|
||||
const normalizedType = normalizeJsonSchemaType(schema)
|
||||
const fieldType = toFieldType(normalizedType, schemaType)
|
||||
|
||||
const field: Field = {
|
||||
type: fieldType,
|
||||
}
|
||||
|
||||
if (schema?.description) field.description = schema.description
|
||||
|
||||
if (schemaType) field.schemaType = schemaType
|
||||
|
||||
if (Array.isArray(schema?.enum)) field.enum = schema.enum
|
||||
|
||||
if (fieldType === Type.object) {
|
||||
const properties = schema?.properties || {}
|
||||
field.properties = Object.entries(properties).reduce(
|
||||
(acc, [key, value]) => {
|
||||
acc[key] = convertJsonSchemaToField(value, schemaTypeDefinitions)
|
||||
return acc
|
||||
},
|
||||
{} as Record<string, Field>,
|
||||
)
|
||||
|
||||
const required = Array.isArray(schema?.required) ? schema.required.filter(Boolean) : undefined
|
||||
field.required = required && required.length > 0 ? required : undefined
|
||||
field.additionalProperties = false
|
||||
}
|
||||
|
||||
if (fieldType === Type.array) {
|
||||
const itemSchema = pickItemSchema(schema)
|
||||
if (itemSchema) {
|
||||
const itemField = convertJsonSchemaToField(itemSchema, schemaTypeDefinitions)
|
||||
const { type, ...rest } = itemField
|
||||
field.items = {
|
||||
...rest,
|
||||
type: toArrayItemType(type),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return field
|
||||
}
|
||||
|
||||
const buildOutputVars = (
|
||||
schema: Record<string, any>,
|
||||
schemaTypeDefinitions?: SchemaDefinitionItemResponse[],
|
||||
): Var[] => {
|
||||
if (!schema || typeof schema !== 'object') return []
|
||||
|
||||
const properties = schema.properties as Record<string, any> | undefined
|
||||
if (!properties) return []
|
||||
|
||||
return Object.entries(properties).map(([name, propertySchema]) => {
|
||||
const { type, schemaType } = resolveVarType(propertySchema, schemaTypeDefinitions)
|
||||
const normalizedType = normalizeJsonSchemaType(propertySchema)
|
||||
|
||||
const varItem: Var = {
|
||||
variable: name,
|
||||
type,
|
||||
des: propertySchema?.description,
|
||||
...(schemaType ? { schemaType } : {}),
|
||||
}
|
||||
|
||||
if (normalizedType === 'object') {
|
||||
const childProperties = propertySchema?.properties
|
||||
? Object.entries(propertySchema.properties).reduce(
|
||||
(acc, [key, value]) => {
|
||||
acc[key] = convertJsonSchemaToField(value, schemaTypeDefinitions)
|
||||
return acc
|
||||
},
|
||||
{} as Record<string, Field>,
|
||||
)
|
||||
: {}
|
||||
|
||||
const required = Array.isArray(propertySchema?.required)
|
||||
? propertySchema.required.filter(Boolean)
|
||||
: undefined
|
||||
|
||||
varItem.children = {
|
||||
schema: {
|
||||
type: Type.object,
|
||||
properties: childProperties,
|
||||
required: required && required.length > 0 ? required : undefined,
|
||||
additionalProperties: false,
|
||||
},
|
||||
} as StructuredOutput
|
||||
}
|
||||
|
||||
return varItem
|
||||
})
|
||||
}
|
||||
|
||||
const metaData = genNodeMetaData({
|
||||
sort: 1,
|
||||
type: BlockEnum.TriggerPlugin,
|
||||
helpLinkUri: 'trigger/plugin-trigger',
|
||||
isStart: true,
|
||||
})
|
||||
|
||||
const nodeDefault: NodeDefault<PluginTriggerNodeType> = {
|
||||
metaData,
|
||||
defaultValue: {
|
||||
plugin_id: '',
|
||||
event_name: '',
|
||||
event_parameters: {},
|
||||
// event_type: '',
|
||||
config: {},
|
||||
},
|
||||
checkValid(
|
||||
payload: PluginTriggerNodeType,
|
||||
t: TFunction<['workflow']>,
|
||||
moreDataForCheckValid: {
|
||||
triggerInputsSchema?: Array<{
|
||||
variable: string
|
||||
label: string
|
||||
required?: boolean
|
||||
}>
|
||||
isReadyForCheckValid?: boolean
|
||||
} = {},
|
||||
) {
|
||||
let errorMessage = ''
|
||||
|
||||
if (!payload.subscription_id)
|
||||
errorMessage = t(($) => $['nodes.triggerPlugin.subscriptionRequired'], { ns: 'workflow' })
|
||||
|
||||
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(($) => $['errorMsg.fieldRequired'], {
|
||||
ns: 'workflow',
|
||||
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(($) => $['errorMsg.fieldRequired'], {
|
||||
ns: 'workflow',
|
||||
field: field.label,
|
||||
})
|
||||
} else {
|
||||
if (
|
||||
value === undefined ||
|
||||
value === null ||
|
||||
value === '' ||
|
||||
(Array.isArray(value) && value.length === 0)
|
||||
) {
|
||||
errorMessage = t(($) => $['errorMsg.fieldRequired'], {
|
||||
ns: 'workflow',
|
||||
field: field.label,
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
isValid: !errorMessage,
|
||||
errorMessage,
|
||||
}
|
||||
},
|
||||
getOutputVars(
|
||||
payload,
|
||||
_allPluginInfoList,
|
||||
_ragVars,
|
||||
{ schemaTypeDefinitions } = { schemaTypeDefinitions: [] },
|
||||
) {
|
||||
const schema = payload.output_schema || {}
|
||||
return buildOutputVars(schema, schemaTypeDefinitions)
|
||||
},
|
||||
}
|
||||
|
||||
export default nodeDefault
|
||||
@@ -0,0 +1,101 @@
|
||||
import type { FC } from 'react'
|
||||
import type { PluginTriggerNodeType } from './types'
|
||||
import type { NodeProps } from '@/app/components/workflow/types'
|
||||
import * as React from 'react'
|
||||
import { useMemo } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import NodeStatus, { NodeStatusEnum } from '@/app/components/base/node-status'
|
||||
import { InstallPluginButton } from '@/app/components/workflow/nodes/_base/components/install-plugin-button'
|
||||
import { useNodePluginInstallation } from '../../hooks/use-node-plugin-installation'
|
||||
import useConfig from './use-config'
|
||||
|
||||
const formatConfigValue = (rawValue: unknown): string => {
|
||||
if (rawValue === null || rawValue === undefined) return ''
|
||||
|
||||
if (typeof rawValue === 'string' || typeof rawValue === 'number' || typeof rawValue === 'boolean')
|
||||
return String(rawValue)
|
||||
|
||||
if (Array.isArray(rawValue)) return rawValue.join('.')
|
||||
|
||||
if (typeof rawValue === 'object') {
|
||||
const { value } = rawValue as { value?: unknown }
|
||||
if (value === null || value === undefined) return ''
|
||||
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean')
|
||||
return String(value)
|
||||
if (Array.isArray(value)) return value.join('.')
|
||||
try {
|
||||
return JSON.stringify(value)
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
const Node: FC<NodeProps<PluginTriggerNodeType>> = ({ id, data }) => {
|
||||
const { subscriptions } = useConfig(id, data)
|
||||
const { config = {}, subscription_id } = data
|
||||
const configKeys = Object.keys(config)
|
||||
const { isChecking, isMissing, uniqueIdentifier, canInstall, onInstallSuccess, shouldDim } =
|
||||
useNodePluginInstallation(data)
|
||||
const showInstallButton = !isChecking && isMissing && canInstall && uniqueIdentifier
|
||||
|
||||
const { t } = useTranslation(['pluginTrigger'])
|
||||
|
||||
const isValidSubscription = useMemo(() => {
|
||||
return subscription_id && subscriptions?.some((sub) => sub.id === subscription_id)
|
||||
}, [subscription_id, subscriptions])
|
||||
|
||||
return (
|
||||
<div className="relative mb-1 px-3 py-1">
|
||||
{showInstallButton && (
|
||||
<div className="pointer-events-auto absolute -top-8 right-3 z-40">
|
||||
<InstallPluginButton
|
||||
size="small"
|
||||
extraIdentifiers={
|
||||
[data.plugin_id, data.provider_id, data.provider_name].filter(Boolean) as string[]
|
||||
}
|
||||
className="font-medium! text-text-accent!"
|
||||
uniqueIdentifier={uniqueIdentifier!}
|
||||
onSuccess={onInstallSuccess}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<div className="space-y-0.5" aria-disabled={shouldDim}>
|
||||
{!isValidSubscription && (
|
||||
<NodeStatus
|
||||
status={NodeStatusEnum.warning}
|
||||
message={t(($) => $['node.status.warning'], { ns: 'pluginTrigger' })}
|
||||
/>
|
||||
)}
|
||||
{isValidSubscription &&
|
||||
configKeys.map((key, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="flex h-6 items-center justify-between space-x-1 rounded-md bg-workflow-block-parma-bg px-1 text-xs font-normal text-text-secondary"
|
||||
>
|
||||
<div
|
||||
title={key}
|
||||
className="max-w-25 shrink-0 truncate text-xs font-medium text-text-tertiary uppercase"
|
||||
>
|
||||
{key}
|
||||
</div>
|
||||
<div
|
||||
title={formatConfigValue(config[key])}
|
||||
className="w-0 shrink-0 grow truncate text-right text-xs font-normal text-text-secondary"
|
||||
>
|
||||
{(() => {
|
||||
const displayValue = formatConfigValue(config[key])
|
||||
if (displayValue.includes('secret')) return '********'
|
||||
return displayValue
|
||||
})()}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(Node)
|
||||
@@ -0,0 +1,93 @@
|
||||
import type { FC } from 'react'
|
||||
import type { PluginTriggerNodeType } from './types'
|
||||
import type { NodePanelProps } from '@/app/components/workflow/types'
|
||||
import * as React from 'react'
|
||||
import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
|
||||
import Split from '@/app/components/workflow/nodes/_base/components/split'
|
||||
import StructureOutputItem from '@/app/components/workflow/nodes/_base/components/variable/object-child-tree-panel/show'
|
||||
import { BlockEnum } from '@/app/components/workflow/types'
|
||||
import { Type } from '../llm/types'
|
||||
import TriggerForm from './components/trigger-form'
|
||||
import useConfig from './use-config'
|
||||
|
||||
const Panel: FC<NodePanelProps<PluginTriggerNodeType>> = ({ id, data }) => {
|
||||
const {
|
||||
readOnly,
|
||||
triggerParameterSchema,
|
||||
triggerParameterValue,
|
||||
setTriggerParameterValue,
|
||||
outputSchema,
|
||||
hasObjectOutput,
|
||||
currentProvider,
|
||||
currentEvent,
|
||||
subscriptionSelected,
|
||||
} = useConfig(id, data)
|
||||
const disableVariableInsertion = data.type === BlockEnum.TriggerPlugin
|
||||
|
||||
// Convert output schema to VarItem format
|
||||
const outputVars = Object.entries(outputSchema.properties || {}).map(
|
||||
([name, schema]: [string, any]) => ({
|
||||
name,
|
||||
type: schema.type || 'string',
|
||||
description: schema.description || '',
|
||||
}),
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="mt-2">
|
||||
{/* Dynamic Parameters Form - Only show when authenticated */}
|
||||
{triggerParameterSchema.length > 0 && subscriptionSelected && (
|
||||
<>
|
||||
<div className="px-4 pb-4">
|
||||
<TriggerForm
|
||||
readOnly={readOnly}
|
||||
nodeId={id}
|
||||
schema={triggerParameterSchema as any}
|
||||
value={triggerParameterValue}
|
||||
onChange={setTriggerParameterValue}
|
||||
currentProvider={currentProvider}
|
||||
currentEvent={currentEvent}
|
||||
disableVariableInsertion={disableVariableInsertion}
|
||||
/>
|
||||
</div>
|
||||
<Split />
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* Output Variables - Always show */}
|
||||
<OutputVars>
|
||||
<>
|
||||
{outputVars.map((varItem) => (
|
||||
<VarItem
|
||||
key={varItem.name}
|
||||
name={varItem.name}
|
||||
type={varItem.type}
|
||||
description={varItem.description}
|
||||
isIndent={hasObjectOutput}
|
||||
/>
|
||||
))}
|
||||
{Object.entries(outputSchema.properties || {}).map(([name, schema]: [string, any]) => (
|
||||
<div key={name}>
|
||||
{schema.type === 'object' ? (
|
||||
<StructureOutputItem
|
||||
rootClassName="code-sm-semibold text-text-secondary"
|
||||
payload={{
|
||||
schema: {
|
||||
type: Type.object,
|
||||
properties: {
|
||||
[name]: schema,
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
))}
|
||||
</>
|
||||
</OutputVars>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(Panel)
|
||||
@@ -0,0 +1,20 @@
|
||||
import type { CollectionType } from '@/app/components/tools/types'
|
||||
import type { ResourceVarInputs } from '@/app/components/workflow/nodes/_base/types'
|
||||
import type { CommonNodeType } from '@/app/components/workflow/types'
|
||||
|
||||
export type PluginTriggerNodeType = CommonNodeType & {
|
||||
provider_id: string
|
||||
provider_type: CollectionType
|
||||
provider_name: string
|
||||
event_name: string
|
||||
event_label: string
|
||||
event_parameters: ResourceVarInputs
|
||||
event_configurations: Record<string, any>
|
||||
output_schema: Record<string, any>
|
||||
parameters_schema?: Record<string, any>[]
|
||||
version?: string
|
||||
event_node_version?: string
|
||||
plugin_id?: string
|
||||
config?: Record<string, any>
|
||||
plugin_unique_identifier?: string
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { PluginTriggerNodeType } from './types'
|
||||
import { useCallback } from 'react'
|
||||
import { getTriggerCheckParams } from '@/app/components/workflow/utils/trigger'
|
||||
import { useGetLanguage } from '@/context/i18n'
|
||||
import { useAllTriggerPlugins } from '@/service/use-triggers'
|
||||
|
||||
type Params = {
|
||||
id: string
|
||||
payload: PluginTriggerNodeType
|
||||
}
|
||||
|
||||
const useGetDataForCheckMore = ({ payload }: Params) => {
|
||||
const { data: triggerPlugins } = useAllTriggerPlugins()
|
||||
const language = useGetLanguage()
|
||||
|
||||
const getData = useCallback(() => {
|
||||
return getTriggerCheckParams(payload, triggerPlugins, language)
|
||||
}, [payload, triggerPlugins, language])
|
||||
|
||||
return {
|
||||
getData,
|
||||
}
|
||||
}
|
||||
|
||||
export default useGetDataForCheckMore
|
||||
@@ -0,0 +1,207 @@
|
||||
import type { PluginTriggerNodeType } from './types'
|
||||
import type { Event } from '@/app/components/tools/types'
|
||||
import type { TriggerWithProvider } from '@/app/components/workflow/block-selector/types'
|
||||
import type { ResourceVarInputs } from '@/app/components/workflow/nodes/_base/types'
|
||||
import type { InputVar } from '@/app/components/workflow/types'
|
||||
import { produce } from 'immer'
|
||||
import { useCallback, useEffect, useMemo } from 'react'
|
||||
import {
|
||||
getConfiguredValue,
|
||||
toolParametersToFormSchemas,
|
||||
} from '@/app/components/tools/utils/to-form-schema'
|
||||
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
|
||||
import { useAllTriggerPlugins, useTriggerSubscriptions } from '@/service/use-triggers'
|
||||
import { useNodesReadOnly } from '../../hooks/use-workflow'
|
||||
import { VarKindType } from '../_base/types'
|
||||
|
||||
const normalizeEventParameters = (
|
||||
params: ResourceVarInputs | Record<string, unknown> | null | undefined,
|
||||
{ allowScalars = false }: { allowScalars?: boolean } = {},
|
||||
): ResourceVarInputs => {
|
||||
if (!params || typeof params !== 'object' || Array.isArray(params)) return {} as ResourceVarInputs
|
||||
|
||||
return Object.entries(params).reduce((acc, [key, entry]) => {
|
||||
if (!entry && entry !== 0 && entry !== false) return acc
|
||||
|
||||
if (typeof entry === 'object' && !Array.isArray(entry) && 'type' in entry && 'value' in entry) {
|
||||
const normalizedEntry = { ...(entry as ResourceVarInputs[string]) }
|
||||
if (normalizedEntry.type === VarKindType.mixed) normalizedEntry.type = VarKindType.constant
|
||||
acc[key] = normalizedEntry
|
||||
return acc
|
||||
}
|
||||
|
||||
if (!allowScalars) return acc
|
||||
|
||||
if (typeof entry === 'string') {
|
||||
acc[key] = {
|
||||
type: VarKindType.constant,
|
||||
value: entry,
|
||||
}
|
||||
return acc
|
||||
}
|
||||
|
||||
if (typeof entry === 'number' || typeof entry === 'boolean') {
|
||||
acc[key] = {
|
||||
type: VarKindType.constant,
|
||||
value: entry,
|
||||
}
|
||||
return acc
|
||||
}
|
||||
|
||||
if (Array.isArray(entry) && entry.every((item) => typeof item === 'string')) {
|
||||
acc[key] = {
|
||||
type: VarKindType.variable,
|
||||
value: entry,
|
||||
}
|
||||
}
|
||||
|
||||
return acc
|
||||
}, {} as ResourceVarInputs)
|
||||
}
|
||||
|
||||
const useConfig = (id: string, payload: PluginTriggerNodeType) => {
|
||||
const { nodesReadOnly: readOnly } = useNodesReadOnly()
|
||||
const { data: triggerPlugins = [] } = useAllTriggerPlugins()
|
||||
|
||||
const { inputs, setInputs: doSetInputs } = useNodeCrud<PluginTriggerNodeType>(id, payload)
|
||||
|
||||
const {
|
||||
provider_id,
|
||||
provider_name,
|
||||
event_name,
|
||||
config = {},
|
||||
event_parameters: rawEventParameters = {},
|
||||
subscription_id,
|
||||
} = inputs
|
||||
|
||||
const event_parameters = useMemo(
|
||||
() => normalizeEventParameters(rawEventParameters),
|
||||
[rawEventParameters],
|
||||
)
|
||||
const legacy_config_parameters = useMemo(
|
||||
() => normalizeEventParameters(config, { allowScalars: true }),
|
||||
[config],
|
||||
)
|
||||
|
||||
const currentProvider = useMemo<TriggerWithProvider | undefined>(() => {
|
||||
return triggerPlugins.find(
|
||||
(provider) =>
|
||||
provider.name === provider_name ||
|
||||
provider.id === provider_id ||
|
||||
(provider_id && provider.plugin_id === provider_id),
|
||||
)
|
||||
}, [triggerPlugins, provider_name, provider_id])
|
||||
|
||||
const { data: subscriptions = [] } = useTriggerSubscriptions(provider_id || '')
|
||||
|
||||
const subscriptionSelected = useMemo(() => {
|
||||
return subscriptions?.find((s) => s.id === subscription_id)
|
||||
}, [subscriptions, subscription_id])
|
||||
|
||||
const currentEvent = useMemo<Event | undefined>(() => {
|
||||
return currentProvider?.events.find((event) => event.name === event_name)
|
||||
}, [currentProvider, event_name])
|
||||
|
||||
// Dynamic trigger parameters (from specific trigger.parameters)
|
||||
const triggerSpecificParameterSchema = useMemo(() => {
|
||||
if (!currentEvent) return []
|
||||
return toolParametersToFormSchemas(currentEvent.parameters)
|
||||
}, [currentEvent])
|
||||
|
||||
// Combined parameter schema (subscription + trigger specific)
|
||||
const triggerParameterSchema = useMemo(() => {
|
||||
const schemaMap = new Map()
|
||||
|
||||
triggerSpecificParameterSchema.forEach((schema) => {
|
||||
schemaMap.set(schema.variable || schema.name, schema)
|
||||
})
|
||||
|
||||
return Array.from(schemaMap.values())
|
||||
}, [triggerSpecificParameterSchema])
|
||||
|
||||
const triggerParameterValue = useMemo(() => {
|
||||
if (!triggerParameterSchema.length) return {} as ResourceVarInputs
|
||||
|
||||
const hasStoredParameters = event_parameters && Object.keys(event_parameters).length > 0
|
||||
const baseValue = hasStoredParameters ? event_parameters : legacy_config_parameters
|
||||
|
||||
const configuredValue = getConfiguredValue(
|
||||
baseValue,
|
||||
triggerParameterSchema,
|
||||
) as ResourceVarInputs
|
||||
return normalizeEventParameters(configuredValue)
|
||||
}, [triggerParameterSchema, event_parameters, legacy_config_parameters])
|
||||
|
||||
useEffect(() => {
|
||||
if (!triggerParameterSchema.length) return
|
||||
|
||||
if (event_parameters && Object.keys(event_parameters).length > 0) return
|
||||
|
||||
if (!triggerParameterValue || Object.keys(triggerParameterValue).length === 0) return
|
||||
|
||||
const newInputs = produce(inputs, (draft) => {
|
||||
draft.event_parameters = triggerParameterValue
|
||||
draft.config = triggerParameterValue
|
||||
})
|
||||
doSetInputs(newInputs)
|
||||
}, [doSetInputs, event_parameters, inputs, triggerParameterSchema, triggerParameterValue])
|
||||
|
||||
const setTriggerParameterValue = useCallback(
|
||||
(value: ResourceVarInputs) => {
|
||||
const sanitizedValue = normalizeEventParameters(value)
|
||||
const newInputs = produce(inputs, (draft) => {
|
||||
draft.event_parameters = sanitizedValue
|
||||
draft.config = sanitizedValue
|
||||
})
|
||||
doSetInputs(newInputs)
|
||||
},
|
||||
[inputs, doSetInputs],
|
||||
)
|
||||
|
||||
const setInputVar = useCallback(
|
||||
(variable: InputVar, varDetail: InputVar) => {
|
||||
const newInputs = produce(inputs, (draft) => {
|
||||
const nextEventParameters = normalizeEventParameters({
|
||||
...draft.event_parameters,
|
||||
[variable.variable]: {
|
||||
type: VarKindType.variable,
|
||||
value: varDetail.variable,
|
||||
},
|
||||
} as ResourceVarInputs)
|
||||
|
||||
draft.event_parameters = nextEventParameters
|
||||
draft.config = nextEventParameters
|
||||
})
|
||||
doSetInputs(newInputs)
|
||||
},
|
||||
[inputs, doSetInputs],
|
||||
)
|
||||
|
||||
// Get output schema
|
||||
const outputSchema = useMemo(() => {
|
||||
return currentEvent?.output_schema || {}
|
||||
}, [currentEvent])
|
||||
|
||||
// Check if trigger has complex output structure
|
||||
const hasObjectOutput = useMemo(() => {
|
||||
const properties = outputSchema.properties || {}
|
||||
return Object.values(properties).some((prop: any) => prop.type === 'object')
|
||||
}, [outputSchema])
|
||||
|
||||
return {
|
||||
readOnly,
|
||||
inputs,
|
||||
currentProvider,
|
||||
currentEvent,
|
||||
triggerParameterSchema,
|
||||
triggerParameterValue,
|
||||
setTriggerParameterValue,
|
||||
setInputVar,
|
||||
outputSchema,
|
||||
hasObjectOutput,
|
||||
subscriptions,
|
||||
subscriptionSelected,
|
||||
}
|
||||
}
|
||||
|
||||
export default useConfig
|
||||
Reference in new issue
Block a user