chore: extend form component for override

pull/12372/head
AkaraChen 1 year ago
parent 1f128729f4
commit 7f5e27d001

@ -39,7 +39,12 @@ type FormProps<
inputClassName?: string inputClassName?: string
isShowDefaultValue?: boolean isShowDefaultValue?: boolean
fieldMoreInfo?: (payload: CredentialFormSchema | CustomFormSchema) => ReactNode fieldMoreInfo?: (payload: CredentialFormSchema | CustomFormSchema) => ReactNode
customRenderField?: (formSchema: CustomFormSchema, props: FormProps<CustomFormSchema>) => ReactNode customRenderField?: (
formSchema: CustomFormSchema,
props: Omit<FormProps<CustomFormSchema>, 'override' | 'customRenderField'>
) => ReactNode
// If return falsy value, this field will fallback to default render
override?: [Array<FormTypeEnum>, (formSchema: CredentialFormSchema) => ReactNode]
} }
function Form< function Form<
@ -60,6 +65,7 @@ function Form<
isShowDefaultValue = false, isShowDefaultValue = false,
fieldMoreInfo, fieldMoreInfo,
customRenderField, customRenderField,
override,
}: FormProps<CustomFormSchema>) { }: FormProps<CustomFormSchema>) {
const language = useLanguage() const language = useLanguage()
const [changeKey, setChangeKey] = useState('') const [changeKey, setChangeKey] = useState('')
@ -97,6 +103,15 @@ function Form<
triggerClassName='ml-1 w-4 h-4' triggerClassName='ml-1 w-4 h-4'
asChild={false} /> asChild={false} />
)) ))
if (override) {
const [overrideTypes, overrideRender] = override
if (overrideTypes.includes(formSchema.type as FormTypeEnum)) {
const node = overrideRender(formSchema as CredentialFormSchema)
if (node)
return node
}
}
if (formSchema.type === FormTypeEnum.textInput || formSchema.type === FormTypeEnum.secretInput || formSchema.type === FormTypeEnum.textNumber) { if (formSchema.type === FormTypeEnum.textInput || formSchema.type === FormTypeEnum.secretInput || formSchema.type === FormTypeEnum.textNumber) {
const { const {
variable, label, placeholder, required, show_on, variable, label, placeholder, required, show_on,
@ -343,7 +358,6 @@ function Form<
inputClassName, inputClassName,
isShowDefaultValue, isShowDefaultValue,
fieldMoreInfo, fieldMoreInfo,
customRenderField,
}) })
} }
} }

@ -39,7 +39,6 @@ function formatStrategy(input: StrategyPluginDetail[], getIcon: (i: string) => s
return input.map((item) => { return input.map((item) => {
const res: ToolWithProvider = { const res: ToolWithProvider = {
id: item.provider, id: item.provider,
// TODO: replace this
author: item.declaration.identity.author, author: item.declaration.identity.author,
name: item.declaration.identity.name, name: item.declaration.identity.name,
description: item.declaration.identity.description as any, description: item.declaration.identity.description as any,

@ -84,7 +84,6 @@ export const AgentStrategy = (props: AgentStrategyProps) => {
} }
} }
} }
console.log(formSchema)
return <div className='space-y-2'> return <div className='space-y-2'>
<AgentStrategySelector value={strategy} onChange={onStrategyChange} /> <AgentStrategySelector value={strategy} onChange={onStrategyChange} />
{ {

@ -32,6 +32,7 @@ import {
} from '@/app/components/workflow/constants' } from '@/app/components/workflow/constants'
import type { PromptItem } from '@/models/debug' import type { PromptItem } from '@/models/debug'
import { VAR_REGEX } from '@/config' import { VAR_REGEX } from '@/config'
import type { AgentNodeType } from '../../../agent/types'
export const isSystemVar = (valueSelector: ValueSelector) => { export const isSystemVar = (valueSelector: ValueSelector) => {
return valueSelector[0] === 'sys' || valueSelector[1] === 'sys' return valueSelector[0] === 'sys' || valueSelector[1] === 'sys'
@ -316,7 +317,18 @@ const formatItem = (
} }
case BlockEnum.Agent: { case BlockEnum.Agent: {
res.vars = [] const payload = data as AgentNodeType
if (!payload.agent_parameters) {
res.vars = []
break
}
res.vars = Object.keys(payload.agent_parameters).map((key) => {
return {
variable: key,
// TODO: is this correct?
type: payload.agent_parameters![key].type as unknown as VarType,
}
})
break break
} }
@ -789,6 +801,14 @@ export const getNodeUsedVars = (node: Node): ValueSelector[] => {
res = [(data as ListFilterNodeType).variable] res = [(data as ListFilterNodeType).variable]
break break
} }
case BlockEnum.Agent: {
const payload = data as AgentNodeType
const params = payload.agent_parameters || {}
const mixVars = matchNotSystemVars(Object.keys(params)?.filter(key => params[key].type === ToolVarType.mixed).map(key => params[key].value) as string[])
const vars = Object.keys(params).filter(key => params[key].type === ToolVarType.variable).map(key => params[key].value as string) || []
res = [...(mixVars as ValueSelector[]), ...(vars as any)]
}
} }
return res || [] return res || []
} }

@ -15,7 +15,7 @@ const nodeDefault: NodeDefault<AgentNodeType> = {
? ALL_CHAT_AVAILABLE_BLOCKS ? ALL_CHAT_AVAILABLE_BLOCKS
: ALL_COMPLETION_AVAILABLE_BLOCKS : ALL_COMPLETION_AVAILABLE_BLOCKS
}, },
checkValid(payload) { checkValid(payload, t, moreDataForCheckValid) {
let isValid = true let isValid = true
let errorMessages = '' let errorMessages = ''
if (payload.type) { if (payload.type) {

@ -1,16 +1,42 @@
import type { FC } from 'react' import { type FC, useMemo } from 'react'
import type { NodeProps } from '../../types' import type { NodeProps } from '../../types'
import type { AgentNodeType } from './types' import type { AgentNodeType } from './types'
import { SettingItem } from '../_base/components/setting-item' import { SettingItem } from '../_base/components/setting-item'
import ModelSelector from '@/app/components/header/account-setting/model-provider-page/model-selector' import ModelSelector from '@/app/components/header/account-setting/model-provider-page/model-selector'
import { Group, GroupLabel } from '../_base/components/group' import { Group, GroupLabel } from '../_base/components/group'
import type { ToolIconProps } from './components/tool-icon'
import { ToolIcon } from './components/tool-icon' import { ToolIcon } from './components/tool-icon'
import useConfig from './use-config' import useConfig from './use-config'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { useInstalledPluginList } from '@/service/use-plugins'
const AgentNode: FC<NodeProps<AgentNodeType>> = (props) => { const AgentNode: FC<NodeProps<AgentNodeType>> = (props) => {
const { inputs } = useConfig(props.id, props.data) const { inputs, currentStrategy } = useConfig(props.id, props.data)
const { t } = useTranslation() const { t } = useTranslation()
const pluginList = useInstalledPluginList()
// TODO: Implement models
const models = useMemo(() => {
const models = []
// if selected, show in node
// if required and not selected, show empty selector
// if not required and not selected, show nothing
}, [currentStrategy, inputs.agent_parameters])
const tools = useMemo(() => {
const tools: Array<ToolIconProps> = []
currentStrategy?.parameters.forEach((param) => {
if (['array[tool]', 'tool'].includes(param.type)) {
const vari = inputs.agent_parameters?.[param.name]
if (!vari) return
if (Array.isArray(vari.value)) {
// TODO: Implement array of tools
}
else {
// TODO: Implement single tool
}
}
})
}, [currentStrategy, inputs.agent_parameters])
return <div className='mb-1 px-3 py-1 space-y-1'> return <div className='mb-1 px-3 py-1 space-y-1'>
{inputs.agent_strategy_name {inputs.agent_strategy_name
? <SettingItem ? <SettingItem

Loading…
Cancel
Save