Skip to content

Commit 4e47d81

Browse files
committed
fix(server): keep exact enum values and allow no custom answer for array enums
1 parent c52779e commit 4e47d81

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,11 @@ 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 }) };
1320+
}
1321+
13171322
export function elicitationFormQuestion(
13181323
id: string,
13191324
property: unknown,
@@ -1325,7 +1330,7 @@ export function elicitationFormQuestion(
13251330
const enumValues = stringEnum(isArray ? unknownRecord(record.items) : record);
13261331
const options =
13271332
enumValues.length > 0
1328-
? enumValues.map((value) => ({ label: value, description: value }))
1333+
? enumValues.map(enumOption)
13291334
: record?.type === "boolean"
13301335
? [
13311336
{ label: "true", description: "Yes" },
@@ -1337,7 +1342,7 @@ export function elicitationFormQuestion(
13371342
header: nonEmptyText(record?.title, `Question ${index + 1}`),
13381343
question: nonEmptyText(record?.description, message),
13391344
options,
1340-
...(isArray && options.length > 0 ? { multiSelect: true } : {}),
1345+
...(isArray && options.length > 0 ? { multiSelect: true, allowCustomAnswer: false } : {}),
13411346
};
13421347
}
13431348

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,24 @@ describe("elicitationFormQuestion", () => {
1919
{ label: "SQLite", description: "SQLite" },
2020
],
2121
multiSelect: true,
22+
allowCustomAnswer: false,
2223
});
2324
});
2425

26+
it("keeps enum values that aren't valid labels exact", () => {
27+
const question = elicitationFormQuestion(
28+
"tag",
29+
{ type: "string", enum: [" padded ", "", "plain"] },
30+
0,
31+
"Pick a tag",
32+
);
33+
assert.deepStrictEqual(question.options, [
34+
{ label: "padded", description: "padded", value: " padded " },
35+
{ label: '""', description: '""', value: "" },
36+
{ label: "plain", description: "plain" },
37+
]);
38+
});
39+
2540
it("keeps single-choice enums, booleans and free text as before", () => {
2641
const single = elicitationFormQuestion("db", { type: "string", enum: ["a", "b"] }, 0, "Pick");
2742
assert.deepStrictEqual(single.options, [

0 commit comments

Comments
 (0)