diff --git a/packages/provider-acp/src/server/elicitationForm.test.ts b/packages/provider-acp/src/server/elicitationForm.test.ts index 3bb0ab52bc8b3..7db2e0da00f0f 100644 --- a/packages/provider-acp/src/server/elicitationForm.test.ts +++ b/packages/provider-acp/src/server/elicitationForm.test.ts @@ -80,6 +80,63 @@ describe("parseElicitationForm", () => { assert.deepStrictEqual(form.content({ mode: "", tags: [""] }), { mode: "", tags: [""] }); }); + it("quotes titled choice values when their labels collide", () => { + const choices = [ + { const: "", title: "" }, + { const: '""', title: '""' }, + { const: "fast", title: "Fast", description: "Quick run" }, + ]; + const form = parseElicitationForm( + decodeElicitationSchema({ + type: "object", + required: ["mode", "tags"], + properties: { + mode: { type: "string", oneOf: choices }, + tags: { type: "array", items: { anyOf: choices } }, + }, + }), + "Configure this run", + ); + assert(form !== undefined); + const expected = [ + ['""', '""', ""], + ['"\\"\\""', '"\\"\\""', '""'], + ["Fast", "Quick run", "fast"], + ]; + assert.deepStrictEqual( + form.questions.map((question) => + question.options.map(({ label, description, value }) => [label, description, value]), + ), + [expected, expected], + ); + assert.deepStrictEqual(form.content({ mode: '""', tags: ["", '""'] }), { + mode: '""', + tags: ["", '""'], + }); + + // A quoted value can collide with another title; every value is quoted then. + const quoted = parseElicitationForm( + { + type: "object", + properties: { + mode: { + type: "string", + oneOf: [ + { const: "a", title: "Same" }, + { const: "b", title: "Same" }, + { const: "c", title: '"a"' }, + ], + }, + }, + }, + "Choose", + ); + assert.deepStrictEqual( + quoted?.questions[0]?.options.map((option) => option.label), + ['"a"', '"b"', '"c"'], + ); + }); + it("asks each question once when a choice has a free-text companion field", () => { // OMP's ask dialog follows each choice `q` with a free-text `q__other` field. const ask = (question: string, header: string) => ({ diff --git a/packages/provider-acp/src/server/elicitationForm.ts b/packages/provider-acp/src/server/elicitationForm.ts index 2b11f37f8ec12..109fc1d73e7d3 100644 --- a/packages/provider-acp/src/server/elicitationForm.ts +++ b/packages/provider-acp/src/server/elicitationForm.ts @@ -51,7 +51,28 @@ function choiceOptions( ): Option[] | undefined { if (!Array.isArray(entries) || entries.length === 0) return undefined; const options = entries.map(toOption); - return options.every((option) => option !== undefined) ? options : undefined; + return options.every((option) => option !== undefined) ? distinctLabels(options) : undefined; +} + +function hasDistinctLabels(options: ReadonlyArray