Skip to content

Commit 0c08bb4

Browse files
committed
fix(server): quote ACP enum labels that would collide after trimming
1 parent 4e47d81 commit 0c08bb4

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,9 +1314,14 @@ function stringEnum(schema: Record<string, unknown> | undefined): ReadonlyArray<
13141314
: [];
13151315
}
13161316

1317-
function enumOption(value: string) {
1318-
const label = value.trim() || JSON.stringify(value);
1319-
return { label, description: label, ...(label === value ? {} : { value }) };
1317+
function enumOptions(values: ReadonlyArray<string>) {
1318+
const trimmed = values.map((value) => value.trim());
1319+
return values.map((value, index) => {
1320+
const plain = trimmed[index]!;
1321+
const unique = plain !== "" && trimmed.indexOf(plain) === trimmed.lastIndexOf(plain);
1322+
const label = unique ? plain : JSON.stringify(value);
1323+
return { label, description: label, ...(label === value ? {} : { value }) };
1324+
});
13201325
}
13211326

13221327
export function elicitationFormQuestion(
@@ -1330,7 +1335,7 @@ export function elicitationFormQuestion(
13301335
const enumValues = stringEnum(isArray ? unknownRecord(record.items) : record);
13311336
const options =
13321337
enumValues.length > 0
1333-
? enumValues.map(enumOption)
1338+
? enumOptions(enumValues)
13341339
: record?.type === "boolean"
13351340
? [
13361341
{ label: "true", description: "Yes" },

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ describe("elicitationFormQuestion", () => {
3737
]);
3838
});
3939

40+
it("quotes enum values whose trimmed labels would collide", () => {
41+
const question = elicitationFormQuestion(
42+
"v",
43+
{ type: "string", enum: [" a", "a", "b"] },
44+
0,
45+
"Pick",
46+
);
47+
assert.deepStrictEqual(question.options, [
48+
{ label: '" a"', description: '" a"', value: " a" },
49+
{ label: '"a"', description: '"a"', value: "a" },
50+
{ label: "b", description: "b" },
51+
]);
52+
});
53+
4054
it("keeps single-choice enums, booleans and free text as before", () => {
4155
const single = elicitationFormQuestion("db", { type: "string", enum: ["a", "b"] }, 0, "Pick");
4256
assert.deepStrictEqual(single.options, [

0 commit comments

Comments
 (0)