@@ -176,6 +176,11 @@ function typedAnswer(
176176 }
177177}
178178
179+ // Some agents offer a free-text "Other" answer beside a choice list by emitting a sibling
180+ // `<id>__other` property. Fold it into the choice question so the composer's custom-answer
181+ // row writes that property instead of the UI asking a separate, easily skipped question.
182+ const OTHER_FIELD_SUFFIX = "__other" ;
183+
179184// Decline unaskable forms; validate types and selection counts, leaving other constraints to the agent.
180185export function parseElicitationForm (
181186 requestedSchema : unknown ,
@@ -187,9 +192,34 @@ export function parseElicitationForm(
187192 ? schema . required . filter ( ( key ) : key is string => typeof key === "string" )
188193 : [ ] ,
189194 ) ;
195+ const propertiesById = unknownRecord ( schema ?. properties ) ?? { } ;
196+ const properties = Object . entries ( propertiesById ) ;
190197 const fields = new Map < string , Field > ( ) ;
191198 const questions : OrchestrationV2UserInputQuestion [ ] = [ ] ;
192- for ( const [ id , value ] of Object . entries ( unknownRecord ( schema ?. properties ) ?? { } ) ) {
199+ const customAnswerFieldIds = new Map < string , string > ( ) ;
200+ const companionIds = new Set < string > ( ) ;
201+ // Resolve the pairing before building questions so a companion is never shown on its own.
202+ for ( const [ id , value ] of properties ) {
203+ if ( ! id . endsWith ( OTHER_FIELD_SUFFIX ) || required . has ( id ) ) continue ;
204+ const baseId = id . slice ( 0 , - OTHER_FIELD_SUFFIX . length ) ;
205+ const baseProperty = unknownRecord ( propertiesById [ baseId ] ) ;
206+ const otherProperty = unknownRecord ( value ) ;
207+ if ( baseProperty === undefined || otherProperty === undefined ) continue ;
208+ const baseField = parseField ( baseProperty ) ;
209+ const otherField = parseField ( otherProperty ) ;
210+ if (
211+ baseField === undefined ||
212+ otherField === undefined ||
213+ ( baseField . field . kind !== "choice" && baseField . field . kind !== "multiChoice" ) ||
214+ otherField . field . kind !== "text"
215+ ) {
216+ continue ;
217+ }
218+ customAnswerFieldIds . set ( baseId , id ) ;
219+ companionIds . add ( id ) ;
220+ }
221+ for ( const [ id , value ] of properties ) {
222+ if ( companionIds . has ( id ) ) continue ;
193223 const property = unknownRecord ( value ) ;
194224 const parsed = property === undefined ? undefined : parseField ( property ) ;
195225 // Question ids must survive the contract's trim to round-trip answers.
@@ -200,9 +230,11 @@ export function parseElicitationForm(
200230 header : text ( property . title ) ?? `Question ${ questions . length + 1 } ` ,
201231 question : text ( property . description ) ?? message ,
202232 options : parsed . options ,
203- ...( parsed . field . kind === "text" || parsed . field . kind === "number"
204- ? { }
205- : { allowCustomAnswer : false } ) ,
233+ ...( customAnswerFieldIds . has ( id )
234+ ? { allowCustomAnswer : true }
235+ : parsed . field . kind === "text" || parsed . field . kind === "number"
236+ ? { }
237+ : { allowCustomAnswer : false } ) ,
206238 ...( parsed . field . kind === "multiChoice" ? { multiSelect : true } : { } ) ,
207239 } ) ;
208240 }
@@ -211,9 +243,20 @@ export function parseElicitationForm(
211243 content : ( answers ) => {
212244 const entries : Array < [ string , EffectAcpSchema . ElicitationContentValue ] > = [ ] ;
213245 for ( const [ id , field ] of fields ) {
214- const typed = Object . hasOwn ( answers , id ) ? typedAnswer ( field , answers [ id ] ) : undefined ;
215- if ( typed !== undefined ) entries . push ( [ id , typed ] ) ;
216- else if ( required . has ( id ) ) return undefined ;
246+ const answer = Object . hasOwn ( answers , id ) ? answers [ id ] : undefined ;
247+ const typed = typedAnswer ( field , answer ) ;
248+ if ( typed !== undefined ) {
249+ entries . push ( [ id , typed ] ) ;
250+ continue ;
251+ }
252+ const customAnswerFieldId = customAnswerFieldIds . get ( id ) ;
253+ const customAnswer =
254+ typeof answer === "string" && answer . trim ( ) . length > 0 ? answer : undefined ;
255+ if ( customAnswerFieldId !== undefined && customAnswer !== undefined ) {
256+ entries . push ( [ customAnswerFieldId , customAnswer ] ) ;
257+ } else if ( required . has ( id ) ) {
258+ return undefined ;
259+ }
217260 }
218261 // Own properties for every field, `__proto__` included; assigning to
219262 // `{}` would hit its inherited setter instead.
0 commit comments