2024-12-26 10:16:52 +08:00
|
|
|
import { useStrategyProviderDetail } from '@/service/use-strategy'
|
2024-12-24 14:20:15 +08:00
|
|
|
import useNodeCrud from '../_base/hooks/use-node-crud'
|
|
|
|
import useVarList from '../_base/hooks/use-var-list'
|
2024-12-31 14:02:50 +08:00
|
|
|
import useOneStepRun from '../_base/hooks/use-one-step-run'
|
2024-12-24 14:20:15 +08:00
|
|
|
import type { AgentNodeType } from './types'
|
|
|
|
import {
|
|
|
|
useNodesReadOnly,
|
|
|
|
} from '@/app/components/workflow/hooks'
|
2025-01-02 15:03:18 +08:00
|
|
|
import { useCallback, useMemo } from 'react'
|
2024-12-30 17:40:56 +08:00
|
|
|
import { type ToolVarInputs, VarType } from '../tool/types'
|
2025-01-03 10:35:06 +08:00
|
|
|
import { useCheckInstalled, useFetchPluginsInMarketPlaceByIds } from '@/service/use-plugins'
|
2025-01-02 15:03:18 +08:00
|
|
|
import type { Var } from '../../types'
|
|
|
|
import { VarType as VarKindType } from '../../types'
|
|
|
|
import useAvailableVarList from '../_base/hooks/use-available-var-list'
|
2024-12-24 14:20:15 +08:00
|
|
|
|
2025-01-03 10:35:06 +08:00
|
|
|
export type StrategyStatus = 'loading' | 'plugin-not-found' | 'plugin-not-found-and-not-in-marketplace' | 'strategy-not-found' | 'success'
|
2025-01-02 16:35:58 +08:00
|
|
|
|
2024-12-24 14:20:15 +08:00
|
|
|
const useConfig = (id: string, payload: AgentNodeType) => {
|
|
|
|
const { nodesReadOnly: readOnly } = useNodesReadOnly()
|
|
|
|
const { inputs, setInputs } = useNodeCrud<AgentNodeType>(id, payload)
|
|
|
|
// variables
|
|
|
|
const { handleVarListChange, handleAddVariable } = useVarList<AgentNodeType>({
|
|
|
|
inputs,
|
|
|
|
setInputs,
|
|
|
|
})
|
2024-12-31 11:52:15 +08:00
|
|
|
const strategyProvider = useStrategyProviderDetail(
|
2024-12-26 10:16:52 +08:00
|
|
|
inputs.agent_strategy_provider_name || '',
|
2025-01-03 10:35:06 +08:00
|
|
|
{ retry: false },
|
2024-12-26 10:16:52 +08:00
|
|
|
)
|
2024-12-31 11:52:15 +08:00
|
|
|
const currentStrategy = strategyProvider.data?.declaration.strategies.find(
|
2024-12-26 10:16:52 +08:00
|
|
|
str => str.identity.name === inputs.agent_strategy_name,
|
|
|
|
)
|
2025-01-03 10:35:06 +08:00
|
|
|
const marketplace = useFetchPluginsInMarketPlaceByIds([inputs.agent_strategy_provider_name!], {
|
|
|
|
retry: false,
|
|
|
|
})
|
2025-01-02 16:35:58 +08:00
|
|
|
const currentStrategyStatus: StrategyStatus = useMemo(() => {
|
2025-01-03 10:35:06 +08:00
|
|
|
if (strategyProvider.isLoading || marketplace.isLoading) return 'loading'
|
|
|
|
if (strategyProvider.isError) {
|
|
|
|
if (marketplace.data && marketplace.data.data.plugins.length === 0)
|
|
|
|
return 'plugin-not-found-and-not-in-marketplace'
|
|
|
|
|
|
|
|
return 'plugin-not-found'
|
|
|
|
}
|
2024-12-31 11:52:15 +08:00
|
|
|
if (!currentStrategy) return 'strategy-not-found'
|
|
|
|
return 'success'
|
2025-01-03 10:35:06 +08:00
|
|
|
}, [currentStrategy, marketplace, strategyProvider.isError, strategyProvider.isLoading])
|
|
|
|
console.log('currentStrategyStatus', currentStrategyStatus)
|
2025-01-02 14:27:19 +08:00
|
|
|
const pluginId = inputs.agent_strategy_provider_name?.split('/').splice(0, 2).join('/')
|
|
|
|
const pluginDetail = useCheckInstalled({
|
|
|
|
pluginIds: [pluginId || ''],
|
|
|
|
enabled: Boolean(pluginId),
|
|
|
|
})
|
2024-12-30 17:40:56 +08:00
|
|
|
const formData = useMemo(() => {
|
|
|
|
return Object.fromEntries(
|
|
|
|
Object.entries(inputs.agent_parameters || {}).map(([key, value]) => {
|
|
|
|
return [key, value.value]
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}, [inputs.agent_parameters])
|
|
|
|
const onFormChange = (value: Record<string, any>) => {
|
|
|
|
const res: ToolVarInputs = {}
|
|
|
|
Object.entries(value).forEach(([key, val]) => {
|
|
|
|
res[key] = {
|
2024-12-31 11:52:15 +08:00
|
|
|
type: VarType.constant,
|
2024-12-30 17:40:56 +08:00
|
|
|
value: val,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
setInputs({
|
|
|
|
...inputs,
|
|
|
|
agent_parameters: res,
|
|
|
|
})
|
|
|
|
}
|
2024-12-31 16:07:53 +08:00
|
|
|
|
2025-01-02 15:03:18 +08:00
|
|
|
// vars
|
|
|
|
|
|
|
|
const filterMemoryPromptVar = useCallback((varPayload: Var) => {
|
|
|
|
return [
|
|
|
|
VarKindType.arrayObject,
|
|
|
|
VarKindType.array,
|
|
|
|
VarKindType.number,
|
|
|
|
VarKindType.string,
|
|
|
|
VarKindType.secret,
|
|
|
|
VarKindType.arrayString,
|
|
|
|
VarKindType.arrayNumber,
|
|
|
|
VarKindType.file,
|
|
|
|
VarKindType.arrayFile,
|
|
|
|
].includes(varPayload.type)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const {
|
|
|
|
availableVars,
|
|
|
|
availableNodesWithParent,
|
|
|
|
} = useAvailableVarList(id, {
|
|
|
|
onlyLeafNodeVar: false,
|
|
|
|
filterVar: filterMemoryPromptVar,
|
|
|
|
})
|
|
|
|
|
2024-12-31 16:07:53 +08:00
|
|
|
// single run
|
|
|
|
const {
|
|
|
|
isShowSingleRun,
|
|
|
|
showSingleRun,
|
|
|
|
hideSingleRun,
|
|
|
|
toVarInputs,
|
|
|
|
runningStatus,
|
|
|
|
handleRun,
|
|
|
|
handleStop,
|
|
|
|
runInputData,
|
|
|
|
setRunInputData,
|
|
|
|
runResult,
|
|
|
|
getInputVars,
|
|
|
|
} = useOneStepRun<AgentNodeType>({
|
|
|
|
id,
|
|
|
|
data: inputs,
|
|
|
|
defaultRunInputData: {},
|
|
|
|
})
|
|
|
|
const allVarStrArr = (() => {
|
2025-01-02 14:55:42 +08:00
|
|
|
const arr = currentStrategy?.parameters.filter(item => item.type === 'string').map((item) => {
|
|
|
|
return formData[item.name]
|
|
|
|
}) || []
|
2024-12-31 16:07:53 +08:00
|
|
|
|
|
|
|
return arr
|
|
|
|
})()
|
|
|
|
const varInputs = (() => {
|
|
|
|
const vars = getInputVars(allVarStrArr)
|
|
|
|
|
|
|
|
return vars
|
|
|
|
})()
|
|
|
|
|
2024-12-24 14:20:15 +08:00
|
|
|
return {
|
|
|
|
readOnly,
|
|
|
|
inputs,
|
2024-12-25 14:52:11 +08:00
|
|
|
setInputs,
|
2024-12-24 14:20:15 +08:00
|
|
|
handleVarListChange,
|
|
|
|
handleAddVariable,
|
2024-12-26 10:16:52 +08:00
|
|
|
currentStrategy,
|
2024-12-30 17:40:56 +08:00
|
|
|
formData,
|
|
|
|
onFormChange,
|
2024-12-31 11:52:15 +08:00
|
|
|
currentStrategyStatus,
|
|
|
|
strategyProvider: strategyProvider.data,
|
2025-01-02 14:27:19 +08:00
|
|
|
pluginDetail: pluginDetail.data?.plugins.at(0),
|
2025-01-02 15:03:18 +08:00
|
|
|
availableVars,
|
|
|
|
availableNodesWithParent,
|
2024-12-31 14:02:50 +08:00
|
|
|
|
|
|
|
isShowSingleRun,
|
|
|
|
showSingleRun,
|
|
|
|
hideSingleRun,
|
|
|
|
toVarInputs,
|
|
|
|
runningStatus,
|
|
|
|
handleRun,
|
|
|
|
handleStop,
|
|
|
|
runInputData,
|
|
|
|
setRunInputData,
|
|
|
|
runResult,
|
2024-12-31 16:07:53 +08:00
|
|
|
varInputs,
|
2024-12-24 14:20:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default useConfig
|