Skip to content

Commit f5094f9

Browse files
committed
fix(opencode): keep ACP question choices and fold the custom answer
OpenCode 2.1.4 sends an ACP elicitation form with a qN choice field (choices in oneOf) and a qN_custom companion for free-form answers. The generic ACP mapping only reads enum and turns every property into its own question, so the card showed two option-less steps: the question and "Type your own answer". Recognize OpenCode's complete form shape at the provider flavor boundary, fold the companion into its question, read titled oneOf/items.anyOf choices, and restore the selected value or typed text on reply. Unrelated forms keep the generic fallback.
1 parent ad068d7 commit f5094f9

4 files changed

Lines changed: 509 additions & 10 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts‎

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ import {
9797

9898
const DEFAULT_GROK_SETTINGS = Schema.decodeSync(GrokSettings)({});
9999
const DROID_SETTINGS = Schema.decodeSync(AcpRegistrySettings)({ agentId: "factory-droid" });
100+
const OPENCODE_SETTINGS = Schema.decodeSync(AcpRegistrySettings)({
101+
source: "local",
102+
commandPath: "/usr/local/bin/opencode",
103+
commandArgs: ["acp"],
104+
});
100105

101106
const layerHost = TestProviderHost.layer().pipe(Layer.provide(NodeServices.layer));
102107

@@ -546,7 +551,7 @@ describe("AcpAdapterV2", () => {
546551
}).pipe(Effect.provide(layerTest), Effect.scoped),
547552
);
548553

549-
// Factory Droid 0.243.0 sends an optional text companion for every AskUser choice.
554+
// Droid 0.243.0 and OpenCode 2.1 pair each choice with an optional free-form companion field.
550555
const droidChoice = {
551556
type: "string",
552557
title: "Format",
@@ -577,6 +582,41 @@ describe("AcpAdapterV2", () => {
577582
],
578583
}));
579584

585+
// OpenCode 2.1 pairs each qN choice with a qN_custom free-form companion.
586+
const openCodeSchema = {
587+
type: "object",
588+
properties: {
589+
q0: {
590+
type: "string",
591+
title: "Color preference",
592+
description: "Which color do you prefer?",
593+
oneOf: [
594+
{ const: "red", title: "Red", description: "Choose the color red" },
595+
{ const: "blue", title: "Blue" },
596+
],
597+
default: "red",
598+
},
599+
q0_custom: {
600+
type: "string",
601+
title: "Color preference (other)",
602+
description: "Type your own answer",
603+
minLength: 1,
604+
},
605+
},
606+
};
607+
const openCodeQuestions = [
608+
{
609+
id: "q0",
610+
header: "Color preference",
611+
question: "Which color do you prefer?",
612+
options: [
613+
{ label: "Red", description: "Choose the color red", value: "red" },
614+
{ label: "Blue", description: "Blue", value: "blue" },
615+
],
616+
allowCustomAnswer: true,
617+
},
618+
];
619+
580620
// regression: #20417 Droid AskUser answers were sent as a cancel
581621
it.live.each([
582622
{
@@ -622,11 +662,46 @@ describe("AcpAdapterV2", () => {
622662
answers: { name: "Ada" },
623663
result: { action: "accept", content: { name: "Ada" } },
624664
},
625-
])("Droid elicitation $scenario", ({ schema, questions, answers, result, withAttachments }) =>
626-
Effect.gen(function* () {
665+
{
666+
scenario: "OpenCode keeps the selected option and hides the custom companion",
667+
settings: OPENCODE_SETTINGS,
668+
schema: openCodeSchema,
669+
questions: openCodeQuestions,
670+
answers: { q0: "blue" },
671+
result: { action: "accept", content: { q0: "blue" } },
672+
},
673+
{
674+
scenario: "OpenCode sends a typed answer to the custom companion",
675+
settings: OPENCODE_SETTINGS,
676+
schema: openCodeSchema,
677+
questions: openCodeQuestions,
678+
answers: { q0: "green" },
679+
result: { action: "accept", content: { q0_custom: "green" } },
680+
},
681+
{
682+
scenario: "OpenCode cancels without accepting answers",
683+
settings: OPENCODE_SETTINGS,
684+
schema: openCodeSchema,
685+
questions: openCodeQuestions,
686+
answers: undefined,
687+
result: { action: "cancel" },
688+
},
689+
{
690+
scenario: "OpenCode falls back for an unrelated form",
691+
settings: OPENCODE_SETTINGS,
692+
schema: { type: "object", properties: { name: { type: "string" } } },
693+
questions: [{ id: "name", header: "Question 1", question: "Your answer?", options: [] }],
694+
answers: { name: "Ada" },
695+
result: { action: "accept", content: { name: "Ada" } },
696+
},
697+
])("Registry elicitation $scenario", (fixture) => {
698+
const { schema, questions, answers, result } = fixture;
699+
const withAttachments = "withAttachments" in fixture && fixture.withAttachments;
700+
const settings = "settings" in fixture ? fixture.settings : DROID_SETTINGS;
701+
return Effect.gen(function* () {
627702
const fileSystem = yield* FileSystem.FileSystem;
628703
const path = yield* Path.Path;
629-
const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-droid-form-" });
704+
const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-registry-form-" });
630705
const statusPath = path.join(directory, "status.json");
631706
const expectedResult = typeof result === "function" ? result(directory) : result;
632707
let preparedAnswers: ProviderUserInputAnswers | undefined = answers;
@@ -730,7 +805,7 @@ describe("AcpAdapterV2", () => {
730805
const adapter = yield* makeAcpRegistryAdapterV2({
731806
instanceId,
732807
selfInvocation: yield* resolveSelfInvocation(),
733-
settings: DROID_SETTINGS,
808+
settings,
734809
environment: {},
735810
makeRuntime: makeAcpReplayRuntime({
736811
transcript,
@@ -746,7 +821,7 @@ describe("AcpAdapterV2", () => {
746821
Layer.mock(AcpRegistryCatalog)({ resolve: () => Effect.die("runtime is injected") }),
747822
),
748823
);
749-
const threadId = ThreadId.make("droid-form-thread");
824+
const threadId = ThreadId.make("registry-form-thread");
750825
const modelSelection = { instanceId, model: "default" } as const;
751826
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
752827
runtimeMode: "approval-required",
@@ -783,7 +858,7 @@ describe("AcpAdapterV2", () => {
783858
),
784859
);
785860
if (item.type !== "turn_item.updated" || item.turnItem.type !== "user_input_request")
786-
return yield* Effect.die("Expected Droid question");
861+
return yield* Effect.die("Expected a user input question");
787862
assert.deepEqual(item.turnItem.questions, questions);
788863
yield* runtime.respondToRuntimeRequest({
789864
requestId: item.turnItem.requestId,
@@ -794,8 +869,8 @@ describe("AcpAdapterV2", () => {
794869
Stream.runHead,
795870
);
796871
assert.equal(Option.getOrThrow(terminal).status, "completed");
797-
}).pipe(Effect.provide(layerTest), Effect.scoped),
798-
);
872+
}).pipe(Effect.provide(layerTest), Effect.scoped);
873+
});
799874

800875
it.live.each(["failed", "recovered", "cancelled"] as const)(
801876
"projects Mistral retry notices and their %s outcome",

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
extractDevinSubagentUpdate,
1010
} from "./devinAcp.ts";
1111
import { extractDroidElicitationForm } from "./droidAcp.ts";
12+
import { extractOpenCodeElicitationForm } from "./opencodeAcp.ts";
1213
import {
1314
copilotClientCapabilitiesMeta,
1415
extractCopilotSubagentUpdate,
@@ -186,6 +187,13 @@ export const makeAcpRegistryAdapterV2 = Effect.fn("makeAcpRegistryAdapterV2")(fu
186187
const startupKey =
187188
options.settings.source === "local" ? `local:${options.instanceId}` : registryAgentId;
188189
const isDevin = registryAgentId === "devin";
190+
const isOpenCode =
191+
registryAgentId === "opencode" ||
192+
registryAgentId === "opencode-v2" ||
193+
(options.settings.source === "local" &&
194+
/^opencode(?:-v2)?(\.exe|\.cmd)?$/i.test(
195+
options.settings.commandPath.split(/[\\/]/).pop() ?? "",
196+
));
189197
// Local installs launch the `copilot` binary directly with no registry id.
190198
const isCopilot =
191199
registryAgentId === "github-copilot-cli" ||
@@ -215,7 +223,9 @@ export const makeAcpRegistryAdapterV2 = Effect.fn("makeAcpRegistryAdapterV2")(fu
215223
promptFailure: (cause) => acpRegistryPromptFailure(registryAgentId, cause),
216224
...(registryAgentId === "factory-droid"
217225
? { extractElicitationForm: extractDroidElicitationForm }
218-
: {}),
226+
: isOpenCode
227+
? { extractElicitationForm: extractOpenCodeElicitationForm }
228+
: {}),
219229
...(registryAgentId === "mistral-vibe"
220230
? { registerExtensions: registerMistralVibeAcpExtensions }
221231
: {}),
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
import type { AcpAdapterV2Flavor } from "@t3tools/provider-acp/server/adapter";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import { extractOpenCodeElicitationForm } from "./opencodeAcp.ts";
5+
6+
type ElicitationRequest = Parameters<NonNullable<AcpAdapterV2Flavor["extractElicitationForm"]>>[0];
7+
8+
const request = (properties: Record<string, unknown>): ElicitationRequest =>
9+
({
10+
mode: "form",
11+
sessionId: "session-id",
12+
message: "Questions",
13+
requestedSchema: { type: "object", properties },
14+
}) as ElicitationRequest;
15+
16+
const colorQuestion = {
17+
type: "string",
18+
title: "Color preference",
19+
description: "Which color do you prefer?",
20+
oneOf: [
21+
{ const: "red", title: "Red", description: "Choose the color red" },
22+
{ const: "blue", title: "Blue" },
23+
],
24+
default: "red",
25+
};
26+
const colorCompanion = {
27+
type: "string",
28+
title: "Color preference (other)",
29+
description: "Type your own answer",
30+
minLength: 1,
31+
};
32+
33+
describe("OpenCode ACP question form", () => {
34+
it("folds the custom-answer companion into its question", () => {
35+
const form = extractOpenCodeElicitationForm(
36+
request({ q0: colorQuestion, q0_custom: colorCompanion }),
37+
);
38+
39+
expect(form?.questions).toEqual([
40+
{
41+
id: "q0",
42+
header: "Color preference",
43+
question: "Which color do you prefer?",
44+
options: [
45+
{ label: "Red", description: "Choose the color red", value: "red" },
46+
{ label: "Blue", description: "Blue", value: "blue" },
47+
],
48+
allowCustomAnswer: true,
49+
},
50+
]);
51+
});
52+
53+
it("returns the selected option value untouched", () => {
54+
const form = extractOpenCodeElicitationForm(
55+
request({ q0: colorQuestion, q0_custom: colorCompanion }),
56+
);
57+
58+
expect(form?.respond({ q0: "blue" })).toEqual({
59+
action: "accept",
60+
content: { q0: "blue" },
61+
});
62+
});
63+
64+
it("sends a free-form answer to the companion field", () => {
65+
const form = extractOpenCodeElicitationForm(
66+
request({ q0: colorQuestion, q0_custom: colorCompanion }),
67+
);
68+
69+
expect(form?.respond({ q0: "green" })).toEqual({
70+
action: "accept",
71+
content: { q0_custom: "green" },
72+
});
73+
});
74+
75+
it("keeps multi-select choices and custom answers together", () => {
76+
const form = extractOpenCodeElicitationForm(
77+
request({
78+
q0: {
79+
type: "array",
80+
title: "Colors",
81+
description: "Which colors?",
82+
items: {
83+
anyOf: [
84+
{ const: "red", title: "Red" },
85+
{ const: "blue", title: "Blue" },
86+
],
87+
},
88+
},
89+
q0_custom: { type: "string", description: "Add your own answer" },
90+
}),
91+
);
92+
93+
expect(form?.questions[0]).toMatchObject({
94+
id: "q0",
95+
multiSelect: true,
96+
allowCustomAnswer: true,
97+
options: [
98+
{ label: "Red", value: "red" },
99+
{ label: "Blue", value: "blue" },
100+
],
101+
});
102+
expect(form?.respond({ q0: ["red", "green"] })).toEqual({
103+
action: "accept",
104+
content: { q0: ["red"], q0_custom: "green" },
105+
});
106+
});
107+
108+
it("keeps multi-select attachment text alongside the selected values", () => {
109+
const form = extractOpenCodeElicitationForm(
110+
request({
111+
q0: {
112+
type: "array",
113+
title: "Colors",
114+
description: "Which colors?",
115+
items: {
116+
anyOf: [
117+
{ const: "red", title: "Red" },
118+
{ const: "blue", title: "Blue" },
119+
],
120+
},
121+
},
122+
q0_custom: { type: "string", description: "Add your own answer" },
123+
}),
124+
);
125+
const suffix = '\n\nAttached image "shot.png": "/tmp/shot.png"';
126+
127+
expect(form?.respond({ q0: ["red", `blue${suffix}`] })).toEqual({
128+
action: "accept",
129+
content: { q0: ["red", "blue"], q0_custom: `Blue${suffix}` },
130+
});
131+
});
132+
133+
it("leaves a text-only form to the generic mapping", () => {
134+
expect(
135+
extractOpenCodeElicitationForm(
136+
request({
137+
q0: {
138+
type: "string",
139+
title: "Name",
140+
description: "What should I call you?",
141+
},
142+
}),
143+
),
144+
).toBeUndefined();
145+
});
146+
147+
it("parses several questions independently", () => {
148+
const form = extractOpenCodeElicitationForm(
149+
request({
150+
q0: colorQuestion,
151+
q0_custom: colorCompanion,
152+
q1: { type: "string", title: "Notes", description: "Anything else?" },
153+
}),
154+
);
155+
156+
expect(form?.questions.map((question) => question.id)).toEqual(["q0", "q1"]);
157+
expect(form?.respond({ q0: "red", q1: "Later" })).toEqual({
158+
action: "accept",
159+
content: { q0: "red", q1: "Later" },
160+
});
161+
});
162+
163+
it("keeps attachment text with the selected option label", () => {
164+
const form = extractOpenCodeElicitationForm(
165+
request({ q0: colorQuestion, q0_custom: colorCompanion }),
166+
);
167+
const suffix = '\n\nAttached image "shot.png": "/tmp/shot.png"';
168+
169+
expect(form?.respond({ q0: `blue${suffix}` })).toEqual({
170+
action: "accept",
171+
content: { q0: "blue", q0_custom: `Blue${suffix}` },
172+
});
173+
});
174+
175+
it("cancels instead of accepting an empty answer", () => {
176+
const form = extractOpenCodeElicitationForm(
177+
request({ q0: colorQuestion, q0_custom: colorCompanion }),
178+
);
179+
180+
expect(form?.respond({})).toEqual({ action: "cancel" });
181+
expect(form?.respond({ q0: " " })).toEqual({ action: "cancel" });
182+
});
183+
184+
it("cancels a multi-value answer to a single-select question", () => {
185+
const form = extractOpenCodeElicitationForm(
186+
request({ q0: colorQuestion, q0_custom: colorCompanion }),
187+
);
188+
189+
expect(form?.respond({ q0: ["red", "blue"] })).toEqual({ action: "cancel" });
190+
});
191+
192+
it("leaves unrelated and incomplete forms to the generic mapping", () => {
193+
expect(
194+
extractOpenCodeElicitationForm(request({ name: { type: "string", title: "Name" } })),
195+
).toBeUndefined();
196+
expect(extractOpenCodeElicitationForm(request({ q0_custom: colorCompanion }))).toBeUndefined();
197+
expect(
198+
extractOpenCodeElicitationForm(
199+
request({ q0: colorQuestion, q0_custom: colorCompanion, extra: { type: "string" } }),
200+
),
201+
).toBeUndefined();
202+
// A malformed choice must not silently become a text-only question.
203+
expect(
204+
extractOpenCodeElicitationForm(
205+
request({
206+
q0: { type: "string", title: "Broken", oneOf: [{ const: "red" }] },
207+
q0_custom: colorCompanion,
208+
}),
209+
),
210+
).toBeUndefined();
211+
});
212+
});

0 commit comments

Comments
 (0)