mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-18 03:35:57 +08:00
feat: agent single run
This commit is contained in:
parent
a6cdcf0632
commit
7242d7bf83
@ -1,4 +1,5 @@
|
|||||||
import { forwardRef, memo, useImperativeHandle } from 'react'
|
import type { FC } from 'react'
|
||||||
|
import { memo } from 'react'
|
||||||
import type { NodePanelProps } from '../../types'
|
import type { NodePanelProps } from '../../types'
|
||||||
import { AgentFeature, type AgentNodeType } from './types'
|
import { AgentFeature, type AgentNodeType } from './types'
|
||||||
import Field from '../_base/components/field'
|
import Field from '../_base/components/field'
|
||||||
@ -8,13 +9,10 @@ import { useTranslation } from 'react-i18next'
|
|||||||
import OutputVars, { VarItem } from '../_base/components/output-vars'
|
import OutputVars, { VarItem } from '../_base/components/output-vars'
|
||||||
import type { StrategyParamItem } from '@/app/components/plugins/types'
|
import type { StrategyParamItem } from '@/app/components/plugins/types'
|
||||||
import type { CredentialFormSchema } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
import type { CredentialFormSchema } from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||||
import { useLogs } from '@/app/components/workflow/run/hooks'
|
|
||||||
import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
|
|
||||||
import { toType } from '@/app/components/tools/utils/to-form-schema'
|
import { toType } from '@/app/components/tools/utils/to-form-schema'
|
||||||
import { useStore } from '../../store'
|
import { useStore } from '../../store'
|
||||||
import Split from '../_base/components/split'
|
import Split from '../_base/components/split'
|
||||||
import MemoryConfig from '../_base/components/memory-config'
|
import MemoryConfig from '../_base/components/memory-config'
|
||||||
import type { PanelExposedType } from '@/types/workflow'
|
|
||||||
const i18nPrefix = 'workflow.nodes.agent'
|
const i18nPrefix = 'workflow.nodes.agent'
|
||||||
|
|
||||||
export function strategyParamToCredientialForm(param: StrategyParamItem): CredentialFormSchema {
|
export function strategyParamToCredientialForm(param: StrategyParamItem): CredentialFormSchema {
|
||||||
@ -27,11 +25,7 @@ export function strategyParamToCredientialForm(param: StrategyParamItem): Creden
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const AgentPanel = forwardRef<PanelExposedType, NodePanelProps<AgentNodeType>>((props, ref) => {
|
const AgentPanel: FC<NodePanelProps<AgentNodeType>> = (props) => {
|
||||||
const {
|
|
||||||
runInputData,
|
|
||||||
setRunInputData,
|
|
||||||
} = props.panelProps
|
|
||||||
const {
|
const {
|
||||||
inputs,
|
inputs,
|
||||||
setInputs,
|
setInputs,
|
||||||
@ -42,39 +36,13 @@ const AgentPanel = forwardRef<PanelExposedType, NodePanelProps<AgentNodeType>>((
|
|||||||
availableNodesWithParent,
|
availableNodesWithParent,
|
||||||
availableVars,
|
availableVars,
|
||||||
readOnly,
|
readOnly,
|
||||||
varInputs,
|
|
||||||
outputSchema,
|
outputSchema,
|
||||||
handleMemoryChange,
|
handleMemoryChange,
|
||||||
} = useConfig(props.id, props.data, props.panelProps)
|
} = useConfig(props.id, props.data)
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
|
|
||||||
const logsParams = useLogs()
|
|
||||||
const singleRunForms = (() => {
|
|
||||||
const forms: FormProps[] = []
|
|
||||||
|
|
||||||
if (varInputs!.length > 0) {
|
|
||||||
forms.push(
|
|
||||||
{
|
|
||||||
label: t(`${i18nPrefix}.singleRun.variable`)!,
|
|
||||||
inputs: varInputs!,
|
|
||||||
values: runInputData,
|
|
||||||
onChange: setRunInputData,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return forms
|
|
||||||
})()
|
|
||||||
|
|
||||||
const resetEditor = useStore(s => s.setControlPromptEditorRerenderKey)
|
const resetEditor = useStore(s => s.setControlPromptEditorRerenderKey)
|
||||||
|
|
||||||
useImperativeHandle(ref, () => ({
|
|
||||||
singleRunParams: {
|
|
||||||
forms: singleRunForms,
|
|
||||||
logsParams,
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
|
|
||||||
return <div className='my-2'>
|
return <div className='my-2'>
|
||||||
<Field title={t('workflow.nodes.agent.strategy.label')} className='px-4 py-2' tooltip={t('workflow.nodes.agent.strategy.tooltip')} >
|
<Field title={t('workflow.nodes.agent.strategy.label')} className='px-4 py-2' tooltip={t('workflow.nodes.agent.strategy.tooltip')} >
|
||||||
<AgentStrategy
|
<AgentStrategy
|
||||||
@ -147,7 +115,7 @@ const AgentPanel = forwardRef<PanelExposedType, NodePanelProps<AgentNodeType>>((
|
|||||||
</OutputVars>
|
</OutputVars>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
})
|
}
|
||||||
|
|
||||||
AgentPanel.displayName = 'AgentPanel'
|
AgentPanel.displayName = 'AgentPanel'
|
||||||
|
|
||||||
|
@ -13,7 +13,6 @@ import type { Memory, Var } from '../../types'
|
|||||||
import { VarType as VarKindType } from '../../types'
|
import { VarType as VarKindType } from '../../types'
|
||||||
import useAvailableVarList from '../_base/hooks/use-available-var-list'
|
import useAvailableVarList from '../_base/hooks/use-available-var-list'
|
||||||
import produce from 'immer'
|
import produce from 'immer'
|
||||||
import type { PanelProps } from '@/types/workflow'
|
|
||||||
|
|
||||||
export type StrategyStatus = {
|
export type StrategyStatus = {
|
||||||
plugin: {
|
plugin: {
|
||||||
@ -63,9 +62,7 @@ export const useStrategyInfo = (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const useConfig = (id: string, payload: AgentNodeType, panelProps?: PanelProps) => {
|
const useConfig = (id: string, payload: AgentNodeType) => {
|
||||||
const getInputVars = panelProps?.getInputVars
|
|
||||||
|
|
||||||
const { nodesReadOnly: readOnly } = useNodesReadOnly()
|
const { nodesReadOnly: readOnly } = useNodesReadOnly()
|
||||||
const { inputs, setInputs } = useNodeCrud<AgentNodeType>(id, payload)
|
const { inputs, setInputs } = useNodeCrud<AgentNodeType>(id, payload)
|
||||||
// variables
|
// variables
|
||||||
@ -133,19 +130,6 @@ const useConfig = (id: string, payload: AgentNodeType, panelProps?: PanelProps)
|
|||||||
|
|
||||||
// single run
|
// single run
|
||||||
|
|
||||||
const allVarStrArr = (() => {
|
|
||||||
const arr = currentStrategy?.parameters.filter(item => item.type === 'string').map((item) => {
|
|
||||||
return formData[item.name]
|
|
||||||
}) || []
|
|
||||||
|
|
||||||
return arr
|
|
||||||
})()
|
|
||||||
const varInputs = (() => {
|
|
||||||
const vars = getInputVars?.(allVarStrArr)
|
|
||||||
|
|
||||||
return vars
|
|
||||||
})()
|
|
||||||
|
|
||||||
const outputSchema = useMemo(() => {
|
const outputSchema = useMemo(() => {
|
||||||
const res: any[] = []
|
const res: any[] = []
|
||||||
if (!inputs.output_schema)
|
if (!inputs.output_schema)
|
||||||
@ -184,7 +168,6 @@ const useConfig = (id: string, payload: AgentNodeType, panelProps?: PanelProps)
|
|||||||
pluginDetail: pluginDetail.data?.plugins.at(0),
|
pluginDetail: pluginDetail.data?.plugins.at(0),
|
||||||
availableVars,
|
availableVars,
|
||||||
availableNodesWithParent,
|
availableNodesWithParent,
|
||||||
varInputs,
|
|
||||||
outputSchema,
|
outputSchema,
|
||||||
handleMemoryChange,
|
handleMemoryChange,
|
||||||
isChatMode,
|
isChatMode,
|
||||||
|
@ -0,0 +1,79 @@
|
|||||||
|
import type { MutableRefObject } from 'react'
|
||||||
|
import type { InputVar, Variable } from '@/app/components/workflow/types'
|
||||||
|
import { useMemo } from 'react'
|
||||||
|
import useNodeCrud from '../_base/hooks/use-node-crud'
|
||||||
|
import type { AgentNodeType } from './types'
|
||||||
|
import { useLogs } from '../../run/hooks'
|
||||||
|
import { useTranslation } from 'react-i18next'
|
||||||
|
import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
|
||||||
|
import { useStrategyInfo } from './use-config'
|
||||||
|
|
||||||
|
const i18nPrefix = 'workflow.nodes.agent'
|
||||||
|
|
||||||
|
type Params = {
|
||||||
|
id: string,
|
||||||
|
payload: AgentNodeType,
|
||||||
|
runInputData: Record<string, any>
|
||||||
|
runInputDataRef: MutableRefObject<Record<string, any>>
|
||||||
|
getInputVars: (textList: string[]) => InputVar[]
|
||||||
|
setRunInputData: (data: Record<string, any>) => void
|
||||||
|
toVarInputs: (variables: Variable[]) => InputVar[]
|
||||||
|
}
|
||||||
|
const useSingleRunFormParams = ({
|
||||||
|
id,
|
||||||
|
payload,
|
||||||
|
runInputData,
|
||||||
|
getInputVars,
|
||||||
|
setRunInputData,
|
||||||
|
}: Params) => {
|
||||||
|
const { t } = useTranslation()
|
||||||
|
const { inputs } = useNodeCrud<AgentNodeType>(id, payload)
|
||||||
|
const logsParams = useLogs()
|
||||||
|
|
||||||
|
const formData = useMemo(() => {
|
||||||
|
return Object.fromEntries(
|
||||||
|
Object.entries(inputs.agent_parameters || {}).map(([key, value]) => {
|
||||||
|
return [key, value.value]
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}, [inputs.agent_parameters])
|
||||||
|
|
||||||
|
const {
|
||||||
|
strategy: currentStrategy,
|
||||||
|
} = useStrategyInfo(
|
||||||
|
inputs.agent_strategy_provider_name,
|
||||||
|
inputs.agent_strategy_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
const allVarStrArr = (() => {
|
||||||
|
const arr = currentStrategy?.parameters.filter(item => item.type === 'string').map((item) => {
|
||||||
|
return formData[item.name]
|
||||||
|
}) || []
|
||||||
|
return arr
|
||||||
|
})()
|
||||||
|
|
||||||
|
const varInputs = getInputVars?.(allVarStrArr)
|
||||||
|
|
||||||
|
const forms = useMemo(() => {
|
||||||
|
const forms: FormProps[] = []
|
||||||
|
|
||||||
|
if (varInputs!.length > 0) {
|
||||||
|
forms.push(
|
||||||
|
{
|
||||||
|
label: t(`${i18nPrefix}.singleRun.variable`)!,
|
||||||
|
inputs: varInputs!,
|
||||||
|
values: runInputData,
|
||||||
|
onChange: setRunInputData,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return forms
|
||||||
|
}, [runInputData, setRunInputData, t, varInputs])
|
||||||
|
|
||||||
|
return {
|
||||||
|
forms,
|
||||||
|
logsParams,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useSingleRunFormParams
|
Loading…
x
Reference in New Issue
Block a user