Skip to content

Commit 6cab423

Browse files
fix(acp): ask forms with a free-text companion field show one step per question
OMP's ask dialog follows each choice `q<n>` with an optional `q<n>__other` text field. The generic ACP form parser made each field its own step, so two questions showed as four steps and the free-text steps had no question text. The parser now asks an optional choice and its optional `<id>__other` text field as one question that accepts a custom answer, and sends typed text back in the companion field. Fixes #27053 AI-assisted; verified with the tests and tools listed above.
1 parent b96ad9c commit 6cab423

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

‎packages/provider-acp/src/server/elicitationForm.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,9 @@ describe("parseElicitationForm", () => {
7575
),
7676
[true, true],
7777
);
78+
assert.deepStrictEqual(form.content({ q0: "Yes", q1: "Only on Fridays" }), {
79+
q0: "Yes",
80+
q1__other: "Only on Fridays",
81+
});
7882
});
7983
});

‎packages/provider-acp/src/server/elicitationForm.ts‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,32 +188,62 @@ export function parseElicitationForm(
188188
: [],
189189
);
190190
const fields = new Map<string, Field>();
191-
const questions: OrchestrationV2UserInputQuestion[] = [];
191+
const parsedFields: Array<{
192+
readonly id: string;
193+
readonly property: Readonly<Record<string, unknown>>;
194+
readonly options: Option[];
195+
}> = [];
192196
for (const [id, value] of Object.entries(unknownRecord(schema?.properties) ?? {})) {
193197
const property = unknownRecord(value);
194198
const parsed = property === undefined ? undefined : parseField(property);
195199
// Question ids must survive the contract's trim to round-trip answers.
196200
if (property === undefined || parsed === undefined || text(id) !== id) return undefined;
197201
fields.set(id, parsed.field);
202+
parsedFields.push({ id, property, options: parsed.options });
203+
}
204+
// An optional `<id>__other` text field (OMP's ask dialog) is the free-text answer to
205+
// optional choice `<id>`: ask them as one question and send custom text to the companion.
206+
const companions = new Map<string, string>();
207+
for (const [id, field] of fields) {
208+
const companionId = `${id}__other`;
209+
if (
210+
(field.kind === "choice" || field.kind === "multiChoice") &&
211+
fields.get(companionId)?.kind === "text" &&
212+
!required.has(id) &&
213+
!required.has(companionId)
214+
) {
215+
companions.set(id, companionId);
216+
}
217+
}
218+
const folded = new Set(companions.values());
219+
const questions: OrchestrationV2UserInputQuestion[] = [];
220+
for (const { id, property, options } of parsedFields) {
221+
if (folded.has(id)) continue;
222+
const kind = fields.get(id)?.kind;
198223
questions.push({
199224
id,
200225
header: text(property.title) ?? `Question ${questions.length + 1}`,
201226
question: text(property.description) ?? message,
202-
options: parsed.options,
203-
...(parsed.field.kind === "text" || parsed.field.kind === "number"
227+
options,
228+
...(kind === "text" || kind === "number" || companions.has(id)
204229
? {}
205230
: { allowCustomAnswer: false }),
206-
...(parsed.field.kind === "multiChoice" ? { multiSelect: true } : {}),
231+
...(kind === "multiChoice" ? { multiSelect: true } : {}),
207232
});
208233
}
209234
return {
210235
questions,
211236
content: (answers) => {
212237
const entries: Array<[string, EffectAcpSchema.ElicitationContentValue]> = [];
213238
for (const [id, field] of fields) {
214-
const typed = Object.hasOwn(answers, id) ? typedAnswer(field, answers[id]) : undefined;
239+
if (folded.has(id)) continue;
240+
const answer = Object.hasOwn(answers, id) ? answers[id] : undefined;
241+
const typed = answer === undefined ? undefined : typedAnswer(field, answer);
242+
const companionId = companions.get(id);
215243
if (typed !== undefined) entries.push([id, typed]);
216-
else if (required.has(id)) return undefined;
244+
else if (companionId !== undefined && typeof answer === "string") {
245+
entries.push([companionId, answer]);
246+
} else if (required.has(id)) return undefined;
217247
}
218248
// Own properties for every field, `__proto__` included; assigning to
219249
// `{}` would hit its inherited setter instead.

0 commit comments

Comments
 (0)