feat: custom credential form

This commit is contained in:
AkaraChen 2024-12-26 14:44:05 +08:00
parent 6f865b96a2
commit 5ef9156242

View File

@ -26,15 +26,21 @@ export type AgentStrategyProps = {
onFormValueChange: (value: ToolVarInputs) => void onFormValueChange: (value: ToolVarInputs) => void
} }
type MaxIterFormSchema = Omit<CredentialFormSchema, 'type'> & { type: 'max-iter' } type CustomSchema<Type, Field = {}> = Omit<CredentialFormSchema, 'type'> & { type: Type } & Field
type MaxIterFormSchema = CustomSchema<'max-iter'>
type ToolSelectorSchema = CustomSchema<'tool-selector'>
type CustomField = MaxIterFormSchema | ToolSelectorSchema
export const AgentStrategy = (props: AgentStrategyProps) => { export const AgentStrategy = (props: AgentStrategyProps) => {
const { strategy, onStrategyChange, formSchema, formValue, onFormValueChange } = props const { strategy, onStrategyChange, formSchema, formValue, onFormValueChange } = props
const { t } = useTranslation() const { t } = useTranslation()
const renderField: ComponentProps<typeof Form<MaxIterFormSchema>>['customRenderField'] = (schema, props) => { const renderField: ComponentProps<typeof Form<CustomField>>['customRenderField'] = (schema, props) => {
switch (schema.type) { switch (schema.type) {
case 'max-iter': { case 'max-iter': {
const value = props.value[schema.variable] const defaultValue = schema.default ? Number.parseInt(schema.default) : 1
const value = props.value[schema.variable] || defaultValue
const onChange = (value: number) => { const onChange = (value: number) => {
props.onChange({ ...props.value, [schema.variable]: value }) props.onChange({ ...props.value, [schema.variable]: value })
} }
@ -45,7 +51,7 @@ export const AgentStrategy = (props: AgentStrategyProps) => {
value={value} value={value}
// TODO: maybe empty, handle this // TODO: maybe empty, handle this
onChange={onChange as any} onChange={onChange as any}
defaultValue={3} defaultValue={defaultValue}
size='sm' size='sm'
min={1} min={1}
max={10} max={10}
@ -63,7 +69,7 @@ export const AgentStrategy = (props: AgentStrategyProps) => {
{ {
strategy strategy
? <div> ? <div>
<Form<MaxIterFormSchema> <Form<CustomField>
formSchemas={[ formSchemas={[
...formSchema, ...formSchema,
{ {
@ -76,6 +82,7 @@ export const AgentStrategy = (props: AgentStrategyProps) => {
name: 'max iter', name: 'max iter',
required: true, required: true,
show_on: [], show_on: [],
default: '3',
} as MaxIterFormSchema, } as MaxIterFormSchema,
]} ]}
value={formValue} value={formValue}