mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
fix(acp): show questions that only mention secrets
This commit is contained in:
@@ -35,6 +35,9 @@ type Input = {
|
||||
export const UnshownQuestionMessage =
|
||||
"The question couldn't be shown to the user in this client. Continue without an answer: make reasonable assumptions and state them, or ask the user in your reply if you can't proceed."
|
||||
|
||||
export const SecretQuestionMessage =
|
||||
"The question wasn't shown to the user because it asks for a typed answer that might be a secret, such as a password, token or API key, and OpenCode doesn't collect secrets through forms in this client. If you don't need a secret, ask again with wording that doesn't mention one or with fixed choices. Otherwise continue without an answer, or ask the user in your reply if you can't proceed."
|
||||
|
||||
function cancel(client: OpenCodeClient, form: Form.Info, message?: string) {
|
||||
return client.session.form.cancel({ sessionID: form.sessionID, formID: form.id, message }).pipe(
|
||||
Effect.catchTag(["FormAlreadySettledError", "FormNotFoundError"], () => Effect.void),
|
||||
@@ -50,8 +53,8 @@ function cancel(client: OpenCodeClient, form: Form.Info, message?: string) {
|
||||
export function requestedSchema(form: AskedForm, capabilities: Capabilities): ElicitationSchema | undefined {
|
||||
if (!capabilities.formElicitation) return undefined
|
||||
if (Option.isNone(Schema.decodeUnknownOption(ElicitedKind)(form.metadata))) return undefined
|
||||
if (form.fields.some(credentialLike)) return undefined
|
||||
const fields = form.fields.filter((field): field is InputField => field.type !== "external")
|
||||
if (form.fields.some(asksForSecret)) return undefined
|
||||
const fields = form.fields.filter((field): field is InputField => field.type !== "external").map(choicesOnly)
|
||||
if (fields.length !== form.fields.length || fields.some((field) => field.when?.length)) return undefined
|
||||
if (fields.some((field) => field.hidden && field.required && field.default === undefined)) return undefined
|
||||
const keys = new Set(fields.map((field) => field.key))
|
||||
@@ -65,10 +68,14 @@ export function requestedSchema(form: AskedForm, capabilities: Capabilities): El
|
||||
}
|
||||
|
||||
// The question tool returns the message to the model, so the turn continues instead of ending as interrupted.
|
||||
export function cancelUnshown(client: OpenCodeClient, form: Form.Info) {
|
||||
return cancel(client, form, form.metadata?.kind === QuestionKind ? UnshownQuestionMessage : undefined).pipe(
|
||||
Effect.uninterruptible,
|
||||
)
|
||||
export function cancelUnshown(client: OpenCodeClient, form: Form.Info, capabilities: Capabilities) {
|
||||
return cancel(client, form, unshownMessage(form, capabilities)).pipe(Effect.uninterruptible)
|
||||
}
|
||||
|
||||
function unshownMessage(form: Form.Info, capabilities: Capabilities) {
|
||||
if (form.metadata?.kind !== QuestionKind) return undefined
|
||||
if (capabilities.formElicitation && form.fields.some(asksForSecret)) return SecretQuestionMessage
|
||||
return UnshownQuestionMessage
|
||||
}
|
||||
|
||||
function answer(form: AskedForm, response: CreateElicitationResponse): Form.Answer | undefined {
|
||||
@@ -108,6 +115,18 @@ export function respond(input: Input, outcome: Form.Answer | "cancel") {
|
||||
)
|
||||
}
|
||||
|
||||
// Only a typed answer can carry a secret, so a field with choices to pick from is still safe to send.
|
||||
function asksForSecret(field: Form.Field) {
|
||||
if (field.type !== "string" && field.type !== "multiselect") return false
|
||||
return (field.options ?? []).length === 0 && credentialLike(field)
|
||||
}
|
||||
|
||||
function choicesOnly(field: InputField): InputField {
|
||||
if (field.type !== "string" && field.type !== "multiselect") return field
|
||||
if (!field.custom || !credentialLike(field)) return field
|
||||
return { ...field, custom: false }
|
||||
}
|
||||
|
||||
function credentialLike(field: Form.Field) {
|
||||
const labels =
|
||||
field.type === "string" || field.type === "multiselect" ? (field.options ?? []).map((option) => option.label) : []
|
||||
|
||||
@@ -170,7 +170,7 @@ export const make = Effect.fnUntraced(function* (input: {
|
||||
return Effect.gen(function* () {
|
||||
const capabilities = yield* Ref.get(input.capabilities)
|
||||
const requestedSchema = ACPElicitation.requestedSchema(output.form, capabilities)
|
||||
if (!requestedSchema) return yield* ACPElicitation.cancelUnshown(input.client, output.form)
|
||||
if (!requestedSchema) return yield* ACPElicitation.cancelUnshown(input.client, output.form, capabilities)
|
||||
const elicitation = {
|
||||
client: input.client,
|
||||
connection: input.connection,
|
||||
|
||||
@@ -66,12 +66,77 @@ describe("acp elicitation mapping", () => {
|
||||
[{ key: "q0", title: "GitHub token", type: "string" }],
|
||||
[{ key: "q0", title: "Password", type: "string", hidden: true, default: "" }],
|
||||
[{ key: "q0", title: "Setup", description: "Paste your API key", type: "string" }],
|
||||
[{ key: "q0", title: "Setup", type: "string", options: [{ value: "a", label: "Use my access token" }] }],
|
||||
[{ key: "q0", title: "Token", type: "string", options: [], custom: true }],
|
||||
]
|
||||
expect(credentials.map((fields) => ACPElicitation.requestedSchema(form(fields), capable))).toEqual(
|
||||
credentials.map(() => undefined),
|
||||
)
|
||||
})
|
||||
|
||||
test("sends choices that mention secrets without a typed answer", () => {
|
||||
expect(
|
||||
ACPElicitation.requestedSchema(
|
||||
form([
|
||||
{
|
||||
key: "q0",
|
||||
title: "Token",
|
||||
description: "Which API token should I use?",
|
||||
type: "string",
|
||||
options: [
|
||||
{ value: "Alpha", label: "Alpha" },
|
||||
{ value: "Beta", label: "Beta" },
|
||||
],
|
||||
custom: true,
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
key: "q1",
|
||||
title: "Logging",
|
||||
description: "Which fields may carry secrets?",
|
||||
type: "multiselect",
|
||||
options: [{ value: "Headers", label: "Headers with access tokens" }],
|
||||
custom: true,
|
||||
},
|
||||
{ key: "q2", title: "Do you have an API key?", type: "boolean" },
|
||||
{ key: "q3", title: "Token budget", type: "integer" },
|
||||
]),
|
||||
capable,
|
||||
),
|
||||
).toEqual({
|
||||
type: "object",
|
||||
properties: {
|
||||
q0: {
|
||||
type: "string",
|
||||
title: "Token",
|
||||
description: "Which API token should I use?",
|
||||
oneOf: [
|
||||
{ const: "Alpha", title: "Alpha", description: undefined },
|
||||
{ const: "Beta", title: "Beta", description: undefined },
|
||||
],
|
||||
default: undefined,
|
||||
},
|
||||
q1: {
|
||||
type: "array",
|
||||
title: "Logging",
|
||||
description: "Which fields may carry secrets?",
|
||||
items: { anyOf: [{ const: "Headers", title: "Headers with access tokens", description: undefined }] },
|
||||
minItems: undefined,
|
||||
maxItems: undefined,
|
||||
default: undefined,
|
||||
},
|
||||
q2: { type: "boolean", title: "Do you have an API key?", description: undefined, default: undefined },
|
||||
q3: {
|
||||
type: "integer",
|
||||
title: "Token budget",
|
||||
description: undefined,
|
||||
minimum: undefined,
|
||||
maximum: undefined,
|
||||
default: undefined,
|
||||
},
|
||||
},
|
||||
required: ["q0"],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("acp elicitation over the wire", () => {
|
||||
@@ -127,4 +192,29 @@ describe("acp elicitation over the wire", () => {
|
||||
expect(acp.server.repliedForms).toEqual([])
|
||||
expect(acp.server.interrupts).toEqual([])
|
||||
})
|
||||
|
||||
test("tells the model why a question asking for a secret wasn't shown", async () => {
|
||||
await using acp = await startSession({
|
||||
capabilities: { elicitation: true },
|
||||
onPrompt: ({ sessionID, id }) => [
|
||||
delivered(sessionID, id),
|
||||
ephemeralEvent("form.created", {
|
||||
form: {
|
||||
id: "frm_secret",
|
||||
sessionID,
|
||||
title: "Questions",
|
||||
metadata: { kind: "question" },
|
||||
fields: [{ key: "q0", title: "Setup", description: "Paste your API key", type: "string" }],
|
||||
},
|
||||
}),
|
||||
],
|
||||
onFormCancel: ({ sessionID }) => [succeeded(sessionID)],
|
||||
})
|
||||
|
||||
expect((await acp.prompt(acp.sessionId, "hello")).stopReason).toBe("end_turn")
|
||||
expect(acp.elicitations).toEqual([])
|
||||
expect(acp.server.cancelledForms).toEqual([
|
||||
{ sessionID: acp.sessionId, formID: "frm_secret", message: ACPElicitation.SecretQuestionMessage },
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -265,7 +265,9 @@ includes `toolCallId` when OpenCode sent that tool call as a `session/update` fo
|
||||
}
|
||||
```
|
||||
|
||||
- OpenCode never sends forms that might collect secrets.
|
||||
- OpenCode never sends forms that might collect secrets. If a question mentions a password, token, API key or similar
|
||||
secret, OpenCode offers only its choices, without a typed answer. If it has no choices, OpenCode doesn't show it and
|
||||
tells the model why.
|
||||
- If a field accepts a typed answer next to its options, the schema adds an optional `<key>_custom` string property.
|
||||
For a single choice, a non-empty custom answer replaces the selected option. For a multiple choice, OpenCode adds it
|
||||
to the selected options.
|
||||
|
||||
Reference in New Issue
Block a user