Skip to content

Commit 3c7d535

Browse files
fix(acp): ACP forms need the requested number of choices before submitting (#48334)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent 4ec5222 commit 3c7d535

6 files changed

Lines changed: 174 additions & 12 deletions

File tree

‎packages/client-runtime/src/state/pendingUserInput.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,37 @@ describe("buildPendingUserInputAnswers", () => {
224224
).toBeNull();
225225
});
226226

227+
it("waits for the provider's minimum number of checked options", () => {
228+
const targets = {
229+
id: "targets",
230+
header: "Targets",
231+
question: "Where should the tests run?",
232+
options: [{ label: "Web" }, { label: "Desktop" }, { label: "Mobile" }],
233+
multiSelect: true,
234+
minSelections: 2,
235+
} as const;
236+
const answers = (selectedOptionValues: ReadonlyArray<string>) =>
237+
buildPendingUserInputAnswers([targets], { targets: { selectedOptionValues } });
238+
239+
expect(answers([])).toEqual({ targets: [] });
240+
expect(answers(["Web"])).toBeNull();
241+
expect(answers(["Web", "Mobile"])).toEqual({ targets: ["Web", "Mobile"] });
242+
243+
const required = { ...targets, required: true };
244+
for (const draft of [
245+
{ customAnswer: "CI" },
246+
{ attachmentCount: 1 },
247+
{ selectedOptionValues: ["Web"], attachmentCount: 1 },
248+
]) {
249+
expect(buildPendingUserInputAnswers([required], { targets: draft })).toBeNull();
250+
}
251+
expect(
252+
buildPendingUserInputAnswers([required], {
253+
targets: { selectedOptionValues: ["Web", "Desktop"], attachmentCount: 1 },
254+
}),
255+
).toEqual({ targets: ["Web", "Desktop"] });
256+
});
257+
227258
it("omits an unselected optional single choice and keeps explicit false and empty choices", () => {
228259
const extras = {
229260
id: "extras",

‎packages/client-runtime/src/state/pendingUserInput.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export interface PendingUserInputQuestion {
33
readonly id: string;
44
readonly options: ReadonlyArray<{ readonly label: string; readonly value?: string | undefined }>;
55
readonly multiSelect?: boolean | undefined;
6+
readonly minSelections?: number | undefined;
67
readonly allowCustomAnswer?: boolean | undefined;
78
readonly initialAnswer?: string | undefined;
89
readonly required?: boolean | undefined;
@@ -63,13 +64,18 @@ export function resolvePendingUserInputAnswer(
6364
draft: PendingUserInputDraftAnswer | undefined,
6465
): PendingUserInputAnswer | null {
6566
if (draft?.attachmentsBlocked) return null;
67+
// Text or attachments cannot stand in for a required minimum of checked options.
68+
const selectionsRequired =
69+
question.multiSelect === true &&
70+
question.required === true &&
71+
question.minSelections !== undefined;
6672
const customAnswer =
6773
question.allowCustomAnswer === false
6874
? null
6975
: question.initialAnswer !== undefined
7076
? (draft?.customAnswer ?? null)
7177
: normalizeDraftAnswer(draft?.customAnswer);
72-
if (customAnswer !== null && customAnswer.length > 0) {
78+
if (customAnswer !== null && customAnswer.length > 0 && !selectionsRequired) {
7379
return customAnswer;
7480
}
7581
if (question.required === false && question.options.length === 0) {
@@ -82,9 +88,12 @@ export function resolvePendingUserInputAnswer(
8288
(question.allowCustomAnswer !== false && (draft?.attachmentCount ?? 0) > 0 ? "" : null);
8389
if (question.multiSelect) {
8490
// Checking nothing answers "none of these" unless the provider requires a choice.
85-
return selectedOptionValues.length > 0
91+
if (selectedOptionValues.length === 0) {
92+
return selectionsRequired ? null : (fallback ?? (question.required ? null : []));
93+
}
94+
return selectedOptionValues.length >= (question.minSelections ?? 1)
8695
? selectedOptionValues
87-
: (fallback ?? (question.required ? null : []));
96+
: null;
8897
}
8998
return selectedOptionValues[0] ?? fallback;
9099
}

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,8 @@ export const OrchestrationV2UserInputQuestion = Schema.Struct({
16601660
}),
16611661
),
16621662
multiSelect: Schema.optional(Schema.Boolean),
1663+
/** The fewest options a non-empty multi-select answer may check. */
1664+
minSelections: Schema.optional(PositiveInt),
16631665
allowCustomAnswer: Schema.optional(Schema.Boolean),
16641666
initialAnswer: Schema.optional(Schema.String),
16651667
required: Schema.optional(Schema.Boolean),

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,24 +169,27 @@ describe("parseElicitationForm", () => {
169169
targets: { type: "array", minItems: 1, items },
170170
extras: { type: "array", items },
171171
platforms: { type: "array", minItems: 1, items },
172+
pair: { type: "array", minItems: 2, items },
172173
},
173-
required: ["targets"],
174+
required: ["targets", "pair"],
174175
},
175176
"Choose",
176177
);
177178
assert(form !== undefined);
178179
assert.deepStrictEqual(
179-
form.questions.map((question) => [question.id, question.required]),
180+
form.questions.map((question) => [question.id, question.required, question.minSelections]),
180181
[
181-
["targets", true],
182-
["extras", undefined],
183-
["platforms", undefined],
182+
["targets", true, undefined],
183+
["extras", undefined, undefined],
184+
["platforms", undefined, undefined],
185+
["pair", true, 2],
184186
],
185187
);
186-
assert.deepStrictEqual(form.content({ targets: ["web"], extras: [], platforms: [] }), {
187-
targets: ["web"],
188-
extras: [],
189-
});
188+
assert.isUndefined(form.content({ targets: ["web"], pair: ["web"] }));
189+
assert.deepStrictEqual(
190+
form.content({ targets: ["web"], extras: [], platforms: [], pair: ["web", "mobile"] }),
191+
{ targets: ["web"], extras: [], pair: ["web", "mobile"] },
192+
);
190193
});
191194

192195
it("marks optional number and integer fields as not required and omits blank answers", () => {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ export function parseElicitationForm(
279279
? {}
280280
: { allowCustomAnswer: false }),
281281
...(kind === "multiChoice" ? { multiSelect: true } : {}),
282+
...(field?.kind === "multiChoice" && field.minItems > 1
283+
? { minSelections: field.minItems }
284+
: {}),
282285
// An empty selection answers "none" unless the schema demands a choice.
283286
...(field?.kind === "multiChoice" && field.minItems > 0 && required.has(id)
284287
? { required: true }
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// @effect-diagnostics nodeBuiltinImport:off -- Seed a local recording for the dev Replay agent.
2+
import * as NodeFSP from "node:fs/promises";
3+
import * as NodePath from "node:path";
4+
5+
import { ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts";
6+
import { expect } from "e2e";
7+
import * as Effect from "effect/Effect";
8+
9+
import { T3_HOME } from "../e2e.config.ts";
10+
import { makeState, rpcClient, runPromise, test, WEB } from "./t3.ts";
11+
12+
const recording = [
13+
{ type: "expect_outbound", frame: { kind: "request", method: "session/prompt" } },
14+
{
15+
type: "emit_inbound",
16+
frame: {
17+
kind: "request",
18+
method: "elicitation/create",
19+
params: {
20+
sessionId: "demo-session",
21+
mode: "form",
22+
message: "Choose where to run the tests.",
23+
requestedSchema: {
24+
type: "object",
25+
properties: {
26+
targets: {
27+
type: "array",
28+
title: "Test targets",
29+
description: "Choose at least two targets.",
30+
items: { type: "string", enum: ["Web", "Desktop", "Mobile"] },
31+
minItems: 2,
32+
},
33+
},
34+
required: ["targets"],
35+
},
36+
},
37+
},
38+
},
39+
{
40+
type: "emit_inbound",
41+
frame: { kind: "response", method: "session/prompt", result: { stopReason: "end_turn" } },
42+
},
43+
]
44+
.map((entry) => JSON.stringify(entry))
45+
.join("\n");
46+
47+
test(
48+
"An ACP question requires the requested number of choices",
49+
WEB,
50+
async ({ t3, app, screen }) => {
51+
const replayAgent = await NodeFSP.readFile(
52+
new URL("../../apps/server/scripts/dev-replay-agent.ts", import.meta.url),
53+
"utf8",
54+
);
55+
const project = await t3.project("Test targets demo", {
56+
"replay/dev-replay-agent.ts": replayAgent,
57+
"replay/dev-replay/showcase_transcript.ndjson": recording,
58+
});
59+
const instanceId = ProviderInstanceId.make("scenario-choice-agent");
60+
await runPromise(
61+
Effect.gen(function* () {
62+
const client = yield* rpcClient(T3_HOME, app.baseUrl ?? "");
63+
yield* client["server.updateSettings"]({
64+
patch: {},
65+
providerInstanceMutation: {
66+
operation: "upsert",
67+
instanceId,
68+
instance: {
69+
driver: ProviderDriverKind.make("acpRegistry"),
70+
displayName: "Choice fixture",
71+
enabled: true,
72+
config: {
73+
source: "local",
74+
commandPath: process.execPath,
75+
commandArgs: [NodePath.join(project, "replay/dev-replay-agent.ts")],
76+
},
77+
},
78+
},
79+
});
80+
}).pipe(Effect.scoped),
81+
);
82+
const { cli } = makeState(T3_HOME);
83+
const started = await runPromise(
84+
cli(
85+
[
86+
"thread",
87+
"start",
88+
project,
89+
"Ask me where to run the tests.",
90+
"--title",
91+
"Choose test targets",
92+
"--provider",
93+
instanceId,
94+
"--model",
95+
"default",
96+
"--wait",
97+
"--json",
98+
],
99+
[2],
100+
),
101+
);
102+
const { threadId } = JSON.parse(started.stdout) as { threadId: string };
103+
await t3.open({ threadId });
104+
105+
const web = screen.getByRole("button", { name: /^Web\b/ });
106+
const submit = screen.getByRole("button", { name: "Submit", exact: true });
107+
await web.tap();
108+
await expect(web).toHaveAttribute("aria-pressed", "true");
109+
await app.screenshot("one-of-two-required-targets");
110+
await expect(submit).toBeDisabled();
111+
await screen.getByRole("button", { name: /^Mobile\b/ }).tap();
112+
await expect(submit).toBeEnabled();
113+
},
114+
);

0 commit comments

Comments
 (0)