@@ -45,8 +45,6 @@ const isCustomCompanion = Schema.is(
4545const questionId = / ^ q \d + $ / ;
4646const companionSuffix = "_custom" ;
4747
48- type Choice = { label : string ; description : string ; value : string } ;
49-
5048// Intake appends file references to a user's answer; keep them with the custom text.
5149const splitAttachments = ( value : string ) => {
5250 const marker = value . indexOf ( "\n\n" ) ;
@@ -59,8 +57,7 @@ const splitAttachments = (value: string) => {
5957type Option = OrchestrationV2UserInputQuestion [ "options" ] [ number ] ;
6058
6159const findChoice = ( options : ReadonlyArray < Option > , value : string ) =>
62- options . find ( ( option ) => ( option . value ?? option . label ) === value ) ??
63- options . find ( ( option ) => option . label === value ) ;
60+ options . find ( ( option ) => option . value === value ) ;
6461
6562const optionValue = ( option : Option ) => option . value ?? option . label ;
6663
@@ -85,47 +82,33 @@ export const extractOpenCodeElicitationForm: NonNullable<
8582 const companion = properties [ `${ id } ${ companionSuffix } ` ] ;
8683 const hasCompanion = companion !== undefined ;
8784 if ( hasCompanion && ! isCustomCompanion ( companion ) ) return undefined ;
88- let header : string ;
89- let description : string | undefined ;
90- let options : Array < Choice > ;
91- let multiSelect = false ;
92- if ( isChoiceQuestion ( field ) ) {
93- header = field . title ;
94- description = field . description ?. trim ( ) ;
95- options = field . oneOf . map ( ( option ) => ( {
96- label : option . title ,
97- description : option . description ?. trim ( ) || option . title ,
98- value : option . const ,
99- } ) ) ;
100- } else if ( isMultiChoiceQuestion ( field ) ) {
101- header = field . title ;
102- description = field . description ?. trim ( ) ;
103- multiSelect = true ;
104- options = field . items . anyOf . map ( ( option ) => ( {
105- label : option . title ,
106- description : option . description ?. trim ( ) || option . title ,
107- value : option . const ,
108- } ) ) ;
109- } else if ( isTextQuestion ( field ) ) {
110- // A choice field we do not fully understand must fall back instead of losing its options.
85+ const fieldChoices = isChoiceQuestion ( field )
86+ ? field . oneOf
87+ : isMultiChoiceQuestion ( field )
88+ ? field . items . anyOf
89+ : undefined ;
90+ if ( fieldChoices === undefined ) {
91+ if ( ! isTextQuestion ( field ) ) return undefined ;
92+ // Unsupported choice schemas belong to the generic mapping.
11193 if ( "enum" in field || "oneOf" in field || "anyOf" in field || "items" in field ) {
11294 return undefined ;
11395 }
114- header = field . title ;
115- description = field . description ?. trim ( ) ;
116- options = [ ] ;
117- } else {
118- return undefined ;
11996 }
97+ if ( ! isTextQuestion ( field ) && ! isMultiChoiceQuestion ( field ) ) return undefined ;
98+ const options = ( fieldChoices ?? [ ] ) . map ( ( option ) => ( {
99+ label : option . title ,
100+ description : option . description ?. trim ( ) || option . title ,
101+ value : option . const ,
102+ } ) ) ;
120103 if ( options . length > 0 ) choices += 1 ;
121104 if ( hasCompanion ) withCompanion . add ( id ) ;
122105 recognized += hasCompanion ? 2 : 1 ;
123106 questions . push ( {
124107 id,
125- header,
126- question : description || header ,
108+ header : field . title ,
109+ question : field . description ?. trim ( ) || field . title ,
127110 options,
128- ...( multiSelect ? { multiSelect : true } : { } ) ,
111+ ...( field . type === "array" ? { multiSelect : true } : { } ) ,
129112 allowCustomAnswer : hasCompanion || options . length === 0 ,
130113 } ) ;
131114 }
@@ -144,60 +127,37 @@ export const extractOpenCodeElicitationForm: NonNullable<
144127 : Predicate . isString ( answer )
145128 ? [ answer ]
146129 : [ ] ;
147- if ( values . length === 0 || values . some ( ( value ) => value . trim ( ) . length === 0 ) ) {
130+ if ( values . length === 0 || ( question . multiSelect !== true && values . length > 1 ) ) {
148131 return { action : "cancel" } ;
149132 }
150133 const companion = withCompanion . has ( question . id ) ;
151- if ( question . multiSelect === true ) {
152- const selected : Array < string > = [ ] ;
153- const custom : Array < string > = [ ] ;
154- for ( const value of values ) {
155- const exact = findChoice ( question . options , value ) ;
156- if ( exact !== undefined ) {
157- selected . push ( optionValue ( exact ) ) ;
158- continue ;
159- }
160- const split = splitAttachments ( value ) ;
161- const attached =
162- split === undefined ? undefined : findChoice ( question . options , split . prefix ) ;
163- if ( attached !== undefined && split !== undefined ) {
164- selected . push ( optionValue ( attached ) ) ;
165- custom . push ( `${ attached . label } ${ split . suffix } ` ) ;
166- continue ;
167- }
168- if ( ! companion ) return { action : "cancel" } ;
134+ const selected : Array < string > = [ ] ;
135+ const custom : Array < string > = [ ] ;
136+ for ( const value of values ) {
137+ const exact = findChoice ( question . options , value ) ;
138+ if ( exact !== undefined ) {
139+ selected . push ( optionValue ( exact ) ) ;
140+ continue ;
141+ }
142+ if ( value . trim ( ) . length === 0 ) return { action : "cancel" } ;
143+ const split = splitAttachments ( value ) ;
144+ const attached =
145+ split === undefined ? undefined : findChoice ( question . options , split . prefix ) ;
146+ if ( companion && attached !== undefined && split !== undefined ) {
147+ selected . push ( optionValue ( attached ) ) ;
148+ custom . push ( `${ attached . label } ${ split . suffix } ` ) ;
149+ } else if ( companion ) {
169150 custom . push ( value ) ;
151+ } else if ( question . options . length === 0 ) {
152+ selected . push ( value ) ;
153+ } else {
154+ return { action : "cancel" } ;
170155 }
171- if ( selected . length > 0 ) content [ question . id ] = selected ;
172- if ( custom . length > 0 ) content [ `${ question . id } ${ companionSuffix } ` ] = custom . join ( "\n" ) ;
173- continue ;
174- }
175- if ( values . length > 1 ) return { action : "cancel" } ;
176- const value = values [ 0 ] ;
177- if ( value === undefined ) return { action : "cancel" } ;
178- const exact = findChoice ( question . options , value ) ;
179- if ( exact !== undefined ) {
180- content [ question . id ] = optionValue ( exact ) ;
181- continue ;
182- }
183- const split = splitAttachments ( value ) ;
184- const attached =
185- split === undefined ? undefined : findChoice ( question . options , split . prefix ) ;
186- if ( attached !== undefined && split !== undefined ) {
187- if ( ! companion ) return { action : "cancel" } ;
188- content [ question . id ] = optionValue ( attached ) ;
189- content [ `${ question . id } ${ companionSuffix } ` ] = `${ attached . label } ${ split . suffix } ` ;
190- continue ;
191- }
192- if ( companion ) {
193- content [ `${ question . id } ${ companionSuffix } ` ] = value ;
194- continue ;
195156 }
196- if ( question . options . length === 0 ) {
197- content [ question . id ] = value ;
198- continue ;
157+ if ( selected . length > 0 ) {
158+ content [ question . id ] = question . multiSelect === true ? selected : selected [ 0 ] ! ;
199159 }
200- return { action : "cancel" } ;
160+ if ( custom . length > 0 ) content [ ` ${ question . id } ${ companionSuffix } ` ] = custom . join ( "\n" ) ;
201161 }
202162 return { action : "accept" , content } ;
203163 } ,
0 commit comments