@@ -13,20 +13,22 @@ import { Separator } from '@/components/ui/separator';
1313import { useBuildSwitchOperatorOptions } from '@/hooks/logic-hooks/use-build-operator-options' ;
1414import { buildOptions } from '@/utils/form' ;
1515import { zodResolver } from '@hookform/resolvers/zod' ;
16- import { memo } from 'react' ;
16+ import { memo , useCallback , useEffect , useMemo } from 'react' ;
1717import { useForm , useWatch } from 'react-hook-form' ;
1818import { useTranslation } from 'react-i18next' ;
1919import { z } from 'zod' ;
2020import {
21+ ArrayFields ,
2122 DataOperationsOperatorOptions ,
22- JsonSchemaDataType ,
2323 ListOperations ,
2424 SortMethod ,
2525 initialListOperationsValues ,
2626} from '../../constant' ;
2727import { useFormValues } from '../../hooks/use-form-values' ;
28+ import { useGetVariableLabelOrTypeByValue } from '../../hooks/use-get-begin-query' ;
2829import { useWatchFormChange } from '../../hooks/use-watch-form-change' ;
2930import { INextOperatorForm } from '../../interface' ;
31+ import { getArrayElementType } from '../../utils' ;
3032import { buildOutputList } from '../../utils/build-output-list' ;
3133import { FormWrapper } from '../components/form-wrapper' ;
3234import { Output , OutputSchema } from '../components/output' ;
@@ -36,7 +38,7 @@ import { QueryVariable } from '../components/query-variable';
3638export const RetrievalPartialSchema = {
3739 query : z . string ( ) ,
3840 operations : z . string ( ) ,
39- n : z . number ( ) . int ( ) . min ( 0 ) . optional ( ) ,
41+ n : z . number ( ) . int ( ) . min ( 1 ) . optional ( ) ,
4042 sort_method : z . string ( ) . optional ( ) ,
4143 filter : z
4244 . object ( {
@@ -47,26 +49,68 @@ export const RetrievalPartialSchema = {
4749 ...OutputSchema ,
4850} ;
4951
52+ const NumFields = [
53+ ListOperations . TopN ,
54+ ListOperations . Head ,
55+ ListOperations . Tail ,
56+ ] ;
57+
58+ function showField ( operations : string ) {
59+ const showNum = NumFields . includes ( operations as ListOperations ) ;
60+ const showSortMethod = [ ListOperations . Sort ] . includes (
61+ operations as ListOperations ,
62+ ) ;
63+ const showFilter = [ ListOperations . Filter ] . includes (
64+ operations as ListOperations ,
65+ ) ;
66+
67+ return {
68+ showNum,
69+ showSortMethod,
70+ showFilter,
71+ } ;
72+ }
73+
5074export const FormSchema = z . object ( RetrievalPartialSchema ) ;
5175
5276export type ListOperationsFormSchemaType = z . infer < typeof FormSchema > ;
5377
54- const outputList = buildOutputList ( initialListOperationsValues . outputs ) ;
55-
5678function ListOperationsForm ( { node } : INextOperatorForm ) {
5779 const { t } = useTranslation ( ) ;
5880
81+ const { getType } = useGetVariableLabelOrTypeByValue ( ) ;
82+
5983 const defaultValues = useFormValues ( initialListOperationsValues , node ) ;
6084
6185 const form = useForm < ListOperationsFormSchemaType > ( {
6286 defaultValues : defaultValues ,
6387 mode : 'onChange' ,
6488 resolver : zodResolver ( FormSchema ) ,
65- shouldUnregister : true ,
89+ // shouldUnregister: true,
6690 } ) ;
6791
6892 const operations = useWatch ( { control : form . control , name : 'operations' } ) ;
6993
94+ const query = useWatch ( { control : form . control , name : 'query' } ) ;
95+
96+ const subType = getArrayElementType ( getType ( query ) ) ;
97+
98+ const currentOutputs = useMemo ( ( ) => {
99+ return {
100+ result : {
101+ type : `Array<${ subType } >` ,
102+ } ,
103+ first : {
104+ type : subType ,
105+ } ,
106+ last : {
107+ type : subType ,
108+ } ,
109+ } ;
110+ } , [ subType ] ) ;
111+
112+ const outputList = buildOutputList ( currentOutputs ) ;
113+
70114 const ListOperationsOptions = buildOptions (
71115 ListOperations ,
72116 t ,
@@ -79,9 +123,39 @@ function ListOperationsForm({ node }: INextOperatorForm) {
79123 `flow.SortMethodOptions` ,
80124 true ,
81125 ) ;
126+
82127 const operatorOptions = useBuildSwitchOperatorOptions (
83128 DataOperationsOperatorOptions ,
84129 ) ;
130+
131+ const { showFilter, showNum, showSortMethod } = showField ( operations ) ;
132+
133+ const handleOperationsChange = useCallback (
134+ ( operations : string ) => {
135+ const { showFilter, showNum, showSortMethod } = showField ( operations ) ;
136+
137+ if ( showNum ) {
138+ form . setValue ( 'n' , 1 , { shouldDirty : true } ) ;
139+ }
140+
141+ if ( showSortMethod ) {
142+ form . setValue ( 'sort_method' , SortMethodOptions . at ( 0 ) ?. value , {
143+ shouldDirty : true ,
144+ } ) ;
145+ }
146+ if ( showFilter ) {
147+ form . setValue ( 'filter.operator' , operatorOptions . at ( 0 ) ?. value , {
148+ shouldDirty : true ,
149+ } ) ;
150+ }
151+ } ,
152+ [ SortMethodOptions , form , operatorOptions ] ,
153+ ) ;
154+
155+ useEffect ( ( ) => {
156+ form . setValue ( 'outputs' , currentOutputs , { shouldDirty : true } ) ;
157+ } , [ currentOutputs , form ] ) ;
158+
85159 useWatchFormChange ( node ?. id , form , true ) ;
86160
87161 return (
@@ -90,37 +164,46 @@ function ListOperationsForm({ node }: INextOperatorForm) {
90164 < QueryVariable
91165 name = "query"
92166 className = "flex-1"
93- types = { [ JsonSchemaDataType . Array ] }
167+ types = { ArrayFields as any [ ] }
94168 > </ QueryVariable >
95169 < Separator />
96170 < RAGFlowFormItem name = "operations" label = { t ( 'flow.operations' ) } >
97- < SelectWithSearch options = { ListOperationsOptions } />
171+ { ( field ) => (
172+ < SelectWithSearch
173+ options = { ListOperationsOptions }
174+ value = { field . value }
175+ onChange = { ( val ) => {
176+ handleOperationsChange ( val ) ;
177+ field . onChange ( val ) ;
178+ } }
179+ />
180+ ) }
98181 </ RAGFlowFormItem >
99- { [
100- ListOperations . TopN ,
101- ListOperations . Head ,
102- ListOperations . Tail ,
103- ] . includes ( operations as ListOperations ) && (
182+ { showNum && (
104183 < FormField
105184 control = { form . control }
106185 name = "n"
107186 render = { ( { field } ) => (
108187 < FormItem >
109- < FormLabel > { t ( 'flowNum' ) } </ FormLabel >
188+ < FormLabel > { t ( 'flow. flowNum' ) } </ FormLabel >
110189 < FormControl >
111- < NumberInput { ...field } className = "w-full" > </ NumberInput >
190+ < NumberInput
191+ { ...field }
192+ className = "w-full"
193+ min = { 1 }
194+ > </ NumberInput >
112195 </ FormControl >
113196 < FormMessage />
114197 </ FormItem >
115198 ) }
116199 />
117200 ) }
118- { [ ListOperations . Sort ] . includes ( operations as ListOperations ) && (
201+ { showSortMethod && (
119202 < RAGFlowFormItem name = "sort_method" label = { t ( 'flow.sortMethod' ) } >
120203 < SelectWithSearch options = { SortMethodOptions } />
121204 </ RAGFlowFormItem >
122205 ) }
123- { [ ListOperations . Filter ] . includes ( operations as ListOperations ) && (
206+ { showFilter && (
124207 < div className = "flex items-center gap-2" >
125208 < RAGFlowFormItem name = "filter.operator" className = "flex-1" >
126209 < SelectWithSearch options = { operatorOptions } > </ SelectWithSearch >
0 commit comments