Skip to content

Commit 52f9938

Browse files
committed
fix(acp): fold Other text fields into elicitation choices
ACP elicitation forms can pair a choice with a free-text Other companion named <id>__other. The parser exposed both properties as separate questions, so picking an option advanced to an empty text question that the pending-input UI would not let the user skip. Pair a non-required <id>__other text property with its choice question. The options stay on one question, the composer enables its custom-answer row, and typed text is written to the companion property the agent reads. Adds a parser test covering titled oneOf/anyOf choices and companions.
1 parent a61cdb5 commit 52f9938

2 files changed

Lines changed: 103 additions & 7 deletions

File tree

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,57 @@ describe("parseElicitationForm", () => {
4444
},
4545
);
4646
}
47+
48+
it("folds a sibling `__other` text field into its choice question", () => {
49+
const form = parseElicitationForm(
50+
{
51+
type: "object",
52+
properties: {
53+
target: {
54+
type: "string",
55+
title: "Which of these should I pick up first?",
56+
description: "Next target",
57+
oneOf: [
58+
{ const: "Land cut", title: "Land cut", description: "Finish the branch" },
59+
{ const: "Audit kit", title: "Audit kit" },
60+
],
61+
default: "Land cut",
62+
},
63+
target__other: { type: "string", title: "Other (type your own)" },
64+
report: {
65+
type: "array",
66+
title: "How much detail do you want back?",
67+
items: { anyOf: [{ const: "Findings", title: "Findings" }] },
68+
},
69+
report__other: { type: "string", title: "Other (type your own)" },
70+
},
71+
},
72+
"Answer 2 questions",
73+
);
74+
75+
assert(form !== undefined);
76+
assert.deepStrictEqual(
77+
form.questions.map((question) => question.id),
78+
["target", "report"],
79+
);
80+
assert.deepStrictEqual(
81+
form.questions[0]?.options.map((option) => option.value),
82+
["Land cut", "Audit kit"],
83+
);
84+
assert.strictEqual(form.questions[0]?.allowCustomAnswer, true);
85+
assert.strictEqual(form.questions[1]?.multiSelect, true);
86+
assert.strictEqual(form.questions[1]?.allowCustomAnswer, true);
87+
88+
// A listed option answers its own property; free text answers the companion.
89+
assert.deepStrictEqual(form.content({ target: "Land cut", report: ["Findings"] }), {
90+
target: "Land cut",
91+
report: ["Findings"],
92+
});
93+
assert.deepStrictEqual(
94+
form.content({ target: "Check the diff first", report: "Just the summary" }),
95+
{ target__other: "Check the diff first", report__other: "Just the summary" },
96+
);
97+
// Neither is required by the agent, so an unanswered question is simply omitted.
98+
assert.deepStrictEqual(form.content({}), {});
99+
});
47100
});

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

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
180185
export 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

Comments
 (0)