Skip to content

Commit fd5cb99

Browse files
QRpeachKZmaria-rcksУвалиев Султан Ермековичfactory-droid[bot]juliusmarminge
authored
fix(opencode): keep ACP question choices and fold the custom answer (pingdotgg#25879)
Co-authored-by: Sultan U <[email protected]> Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Увалиев Султан Ермекович <[email protected]> Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> Co-authored-by: Julius Marminge <[email protected]>
1 parent 6d53584 commit fd5cb99

4 files changed

Lines changed: 349 additions & 10 deletions

File tree

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

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ import {
102102

103103
const DEFAULT_GROK_SETTINGS = Schema.decodeSync(GrokSettings)({});
104104
const DROID_SETTINGS = Schema.decodeSync(AcpRegistrySettings)({ agentId: "factory-droid" });
105+
const OPENCODE_SETTINGS = Schema.decodeSync(AcpRegistrySettings)({
106+
source: "local",
107+
commandPath: "/usr/local/bin/opencode",
108+
commandArgs: ["acp"],
109+
});
105110

106111
const layerHost = TestProviderHost.layer().pipe(
107112
Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
@@ -1518,7 +1523,7 @@ describe("AcpAdapterV2", () => {
15181523
}).pipe(Effect.provide(layerTest), Effect.scoped),
15191524
);
15201525

1521-
// Factory Droid 0.243.0 sends an optional text companion for every AskUser choice.
1526+
// Droid 0.243.0 and OpenCode 2.1 pair each choice with an optional free-form companion field.
15221527
const droidChoice = {
15231528
type: "string",
15241529
title: "Format",
@@ -1549,6 +1554,41 @@ describe("AcpAdapterV2", () => {
15491554
],
15501555
}));
15511556

1557+
// OpenCode 2.1 pairs each qN choice with a qN_custom free-form companion.
1558+
const openCodeSchema = {
1559+
type: "object",
1560+
properties: {
1561+
q0: {
1562+
type: "string",
1563+
title: "Color preference",
1564+
description: "Which color do you prefer?",
1565+
oneOf: [
1566+
{ const: "red", title: "Red", description: "Choose the color red" },
1567+
{ const: "blue", title: "Blue" },
1568+
],
1569+
default: "red",
1570+
},
1571+
q0_custom: {
1572+
type: "string",
1573+
title: "Color preference (other)",
1574+
description: "Type your own answer",
1575+
minLength: 1,
1576+
},
1577+
},
1578+
};
1579+
const openCodeQuestions = [
1580+
{
1581+
id: "q0",
1582+
header: "Color preference",
1583+
question: "Which color do you prefer?",
1584+
options: [
1585+
{ label: "Red", description: "Choose the color red", value: "red" },
1586+
{ label: "Blue", description: "Blue", value: "blue" },
1587+
],
1588+
allowCustomAnswer: true,
1589+
},
1590+
];
1591+
15521592
// regression: #20417 Droid AskUser answers were sent as a cancel
15531593
it.live.each([
15541594
{
@@ -1594,11 +1634,22 @@ describe("AcpAdapterV2", () => {
15941634
answers: { name: "Ada" },
15951635
result: { action: "accept", content: { name: "Ada" } },
15961636
},
1597-
])("Droid elicitation $scenario", ({ schema, questions, answers, result, withAttachments }) =>
1598-
Effect.gen(function* () {
1637+
{
1638+
scenario: "OpenCode keeps the selected option and hides the custom companion",
1639+
settings: OPENCODE_SETTINGS,
1640+
schema: openCodeSchema,
1641+
questions: openCodeQuestions,
1642+
answers: { q0: "blue" },
1643+
result: { action: "accept", content: { q0: "blue" } },
1644+
},
1645+
])("Registry elicitation $scenario", (fixture) => {
1646+
const { schema, questions, answers, result } = fixture;
1647+
const withAttachments = "withAttachments" in fixture && fixture.withAttachments;
1648+
const settings = "settings" in fixture ? fixture.settings : DROID_SETTINGS;
1649+
return Effect.gen(function* () {
15991650
const fileSystem = yield* FileSystem.FileSystem;
16001651
const path = yield* Path.Path;
1601-
const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-droid-form-" });
1652+
const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-registry-form-" });
16021653
const statusPath = path.join(directory, "status.json");
16031654
const expectedResult = typeof result === "function" ? result(directory) : result;
16041655
let preparedAnswers: ProviderUserInputAnswers | undefined = answers;
@@ -1702,7 +1753,7 @@ describe("AcpAdapterV2", () => {
17021753
const adapter = yield* makeAcpRegistryAdapterV2({
17031754
instanceId,
17041755
selfInvocation: yield* resolveSelfInvocation(),
1705-
settings: DROID_SETTINGS,
1756+
settings,
17061757
environment: {},
17071758
makeRuntime: makeAcpReplayRuntime({
17081759
transcript,
@@ -1718,7 +1769,7 @@ describe("AcpAdapterV2", () => {
17181769
Layer.mock(AcpRegistryCatalog)({ resolve: () => Effect.die("runtime is injected") }),
17191770
),
17201771
);
1721-
const threadId = ThreadId.make("droid-form-thread");
1772+
const threadId = ThreadId.make("registry-form-thread");
17221773
const modelSelection = { instanceId, model: "default" } as const;
17231774
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
17241775
runtimeMode: "approval-required",
@@ -1755,7 +1806,7 @@ describe("AcpAdapterV2", () => {
17551806
),
17561807
);
17571808
if (item.type !== "turn_item.updated" || item.turnItem.type !== "user_input_request")
1758-
return yield* Effect.die("Expected Droid question");
1809+
return yield* Effect.die("Expected a user input question");
17591810
assert.deepEqual(item.turnItem.questions, questions);
17601811
yield* runtime.respondToRuntimeRequest({
17611812
requestId: item.turnItem.requestId,
@@ -1766,8 +1817,8 @@ describe("AcpAdapterV2", () => {
17661817
Stream.runHead,
17671818
);
17681819
assert.equal(Option.getOrThrow(terminal).status, "completed");
1769-
}).pipe(Effect.provide(layerTest), Effect.scoped),
1770-
);
1820+
}).pipe(Effect.provide(layerTest), Effect.scoped);
1821+
});
17711822

17721823
it.live.each(["failed", "recovered", "cancelled"] as const)(
17731824
"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,
@@ -187,6 +188,13 @@ export const makeAcpRegistryAdapterV2 = Effect.fn("makeAcpRegistryAdapterV2")(fu
187188
const startupKey =
188189
options.settings.source === "local" ? `local:${options.instanceId}` : registryAgentId;
189190
const isDevin = registryAgentId === "devin";
191+
const isOpenCode =
192+
registryAgentId === "opencode" ||
193+
registryAgentId === "opencode-v2" ||
194+
(options.settings.source === "local" &&
195+
/^opencode(?:-v2)?(\.exe|\.cmd)?$/i.test(
196+
options.settings.commandPath.split(/[\\/]/).pop() ?? "",
197+
));
190198
// Local installs launch the `copilot` binary directly with no registry id.
191199
const isCopilot =
192200
registryAgentId === "github-copilot-cli" ||
@@ -222,7 +230,9 @@ export const makeAcpRegistryAdapterV2 = Effect.fn("makeAcpRegistryAdapterV2")(fu
222230
promptFailure: (cause) => acpRegistryPromptFailure(registryAgentId, cause),
223231
...(registryAgentId === "factory-droid"
224232
? { extractElicitationForm: extractDroidElicitationForm }
225-
: {}),
233+
: isOpenCode
234+
? { extractElicitationForm: extractOpenCodeElicitationForm }
235+
: {}),
226236
// glm-acp-agent is the one agent whose per-turn usage a live probe has verified.
227237
...(registryAgentId === "glm-acp-agent" ? { reportsPerTurnPromptUsage: true } : {}),
228238
...(registryAgentId === "mistral-vibe"
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
const multiColorQuestion = {
33+
type: "array",
34+
title: "Colors",
35+
description: "Which colors?",
36+
items: {
37+
anyOf: [
38+
{ const: "red", title: "Red" },
39+
{ const: "blue", title: "Blue" },
40+
],
41+
},
42+
};
43+
const multiColorCompanion = { type: "string", description: "Add your own answer" };
44+
const attachment = '\n\nAttached image "shot.png": "/tmp/shot.png"';
45+
46+
describe("OpenCode ACP question form", () => {
47+
it("folds the custom-answer companion into its question", () => {
48+
const form = extractOpenCodeElicitationForm(
49+
request({
50+
q0: colorQuestion,
51+
q0_custom: colorCompanion,
52+
q1: { type: "string", title: "Notes", description: "Anything else?" },
53+
}),
54+
);
55+
56+
expect(form?.questions).toEqual([
57+
{
58+
id: "q0",
59+
header: "Color preference",
60+
question: "Which color do you prefer?",
61+
options: [
62+
{ label: "Red", description: "Choose the color red", value: "red" },
63+
{ label: "Blue", description: "Blue", value: "blue" },
64+
],
65+
allowCustomAnswer: true,
66+
},
67+
{
68+
id: "q1",
69+
header: "Notes",
70+
question: "Anything else?",
71+
options: [],
72+
allowCustomAnswer: true,
73+
},
74+
]);
75+
expect(form?.respond({ q0: "blue", q1: "Later" })).toEqual({
76+
action: "accept",
77+
content: { q0: "blue", q1: "Later" },
78+
});
79+
});
80+
81+
it("prefers an option value over an earlier option label", () => {
82+
const form = extractOpenCodeElicitationForm(
83+
request({
84+
q0: {
85+
...colorQuestion,
86+
oneOf: [
87+
{ const: "a", title: "b" },
88+
{ const: "b", title: "B" },
89+
],
90+
},
91+
q0_custom: colorCompanion,
92+
}),
93+
);
94+
95+
expect(form?.respond({ q0: "b" })).toEqual({ action: "accept", content: { q0: "b" } });
96+
});
97+
98+
it("sends a free-form answer to the companion field", () => {
99+
const form = extractOpenCodeElicitationForm(
100+
request({ q0: colorQuestion, q0_custom: colorCompanion }),
101+
);
102+
103+
expect(form?.respond({ q0: "green" })).toEqual({
104+
action: "accept",
105+
content: { q0_custom: "green" },
106+
});
107+
});
108+
109+
it("keeps attachment text with the selected option", () => {
110+
const form = extractOpenCodeElicitationForm(
111+
request({ q0: colorQuestion, q0_custom: colorCompanion }),
112+
);
113+
114+
expect(form?.respond({ q0: `blue${attachment}` })).toEqual({
115+
action: "accept",
116+
content: { q0: "blue", q0_custom: `Blue${attachment}` },
117+
});
118+
});
119+
120+
it("cancels empty or multi-value answers to a single-select question", () => {
121+
const form = extractOpenCodeElicitationForm(
122+
request({ q0: colorQuestion, q0_custom: colorCompanion }),
123+
);
124+
125+
expect(form?.respond({})).toEqual({ action: "cancel" });
126+
expect(form?.respond({ q0: " " })).toEqual({ action: "cancel" });
127+
expect(form?.respond({ q0: ["red", "blue"] })).toEqual({ action: "cancel" });
128+
});
129+
130+
it.each([
131+
["a text-only form", { q0: { type: "string", title: "Name", description: "Who?" } }],
132+
["an unrelated field", { q0: colorQuestion, q0_custom: colorCompanion, extra: {} }],
133+
["a multi-select choice", { q0: multiColorQuestion, q0_custom: multiColorCompanion }],
134+
["an orphan companion", { q0_custom: colorCompanion }],
135+
["an enum choice", { q0: colorQuestion, q1: { type: "string", title: "Size", enum: ["S"] } }],
136+
[
137+
"a choice without a title",
138+
{ q0: colorQuestion, q1: { ...colorQuestion, oneOf: [{ const: "red" }] } },
139+
],
140+
])("leaves %s to the generic mapping", (_, properties) => {
141+
expect(extractOpenCodeElicitationForm(request(properties))).toBeUndefined();
142+
});
143+
});

0 commit comments

Comments
 (0)