From 1d610f90a04817c3e8b467f879e4d9a24daa0aff Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Tue, 6 Oct 2026 22:41:21 -0500 Subject: [PATCH] fix(ui): support inline custom answers for string choice fields in connect (#53625) --- packages/app/src/providers/connect/dialog.tsx | 156 +++++++++++--- .../tui/src/component/dialog-integration.tsx | 190 ++++++++++++++++-- 2 files changed, 302 insertions(+), 44 deletions(-) diff --git a/packages/app/src/providers/connect/dialog.tsx b/packages/app/src/providers/connect/dialog.tsx index 42ab21d6fa3..6c4b4fda600 100644 --- a/packages/app/src/providers/connect/dialog.tsx +++ b/packages/app/src/providers/connect/dialog.tsx @@ -3,6 +3,7 @@ import { Badge } from "@opencode/ui/badge" import { useDialog } from "@opencode/ui/context/dialog" import { Icon } from "@opencode/ui/icon" import { List } from "@opencode/ui/list" +import { RadioGroup, RadioItem } from "@opencode/ui/radio" import { Spinner } from "@opencode/ui/spinner" import { TextField } from "@opencode/ui/text-field" import { DialogBody, DialogHeader, DialogTitle, Dialog } from "@opencode/ui/dialog" @@ -61,7 +62,7 @@ type IntegrationForm = NonNullable[number] type StringForm = Extract export function useProviderConnectController() { - const [store, setStore] = createStore({ selected: undefined as string | undefined }) + const [store, setStore] = createStore<{ selected?: string }>({}) const reset = () => setStore("selected", undefined) return { @@ -83,9 +84,13 @@ export const DialogConnectProvider: Component<{ const fallback = useProviderConnectController() const controller = props.controller ?? fallback - const [state, setState] = createStore({ + const [state, setState] = createStore<{ + completed: boolean + modelProvider?: { id: string; name: string } + authorization: boolean + chatgptWelcome: boolean + }>({ completed: false, - modelProvider: undefined as { id: string; name: string } | undefined, authorization: false, chatgptWelcome: false, }) @@ -216,10 +221,12 @@ function ProviderPicker(props: { directory?: string; onSelect: (provider: string const integrations = useIntegrations(() => props.directory) const language = useLanguage() - const [store, setStore] = createStore({ + const [store, setStore] = createStore<{ + filter: string + active?: string + connecting?: string + }>({ filter: "", - active: undefined as string | undefined, - connecting: undefined as string | undefined, }) const featured = ["opencode-go", "opencode", "anthropic", "openai", "google", "openrouter", "vercel"] @@ -421,16 +428,24 @@ function ProviderConnection(props: { const isConsole = CONSOLE_PROVIDERS.has(props.provider) const remote = isConsole && authServerName(sdk.server) !== undefined - const [state, setState] = createStore({ + const [state, setState] = createStore<{ + copied: boolean + copyFailed: boolean + firstConnection?: boolean + models: boolean + noModels: boolean + catalogPending: boolean + selectedModel: string + collapsed: Record + }>({ copied: false, copyFailed: false, - firstConnection: undefined as boolean | undefined, models: false, noModels: false, // The workspace providers had not loaded when the wait ran out. catalogPending: false, selectedModel: "", - collapsed: {} as Record, + collapsed: {}, }) const controller = createProviderConnectionController({ @@ -642,11 +657,22 @@ function ProviderConnection(props: { function AuthFormView() { const defaults = providerFormDefaults(controller.currentMethod()?.form) - const [formStore, setFormStore] = createStore({ + const [formStore, setFormStore] = createStore<{ + value: Record + index: number + customOn: boolean + customText: string + }>({ value: Object.fromEntries( - Object.entries(defaults).flatMap(([key, value]) => (typeof value === "string" ? [[key, value]] : [])), - ) as Record, + (controller.currentMethod()?.form ?? []).flatMap((field) => + field.type === "string" && field.key in defaults && field.default !== undefined + ? [[field.key, field.default]] + : [], + ), + ), index: 0, + customOn: false, + customText: "", }) const fields = createMemo(() => { @@ -677,14 +703,18 @@ function ProviderConnection(props: { } }) + // Visible defaults prefill the field but enter the answer only once that field is answered, + // because the server rejects values for fields whose conditions are not met. + const answer = (field: StringForm) => formStore.value[field.key] ?? field.default + const valid = createMemo(() => { const item = current() - if (!item || item.field.options) return false + if (!item || (item.field.options && !item.field.custom)) return false if (!item.field.required) return true - return (formStore.value[item.field.key] ?? "").trim().length > 0 + return (answer(item.field) ?? "").trim().length > 0 }) async function next(index: number, value: Record) { @@ -694,7 +724,7 @@ function ProviderConnection(props: { const next = fields().findIndex((field, i) => i > index && matches(field, value)) if (next !== -1) { - setFormStore("index", next) + setFormStore({ index: next, customOn: false, customText: "" }) return } @@ -706,9 +736,10 @@ function ProviderConnection(props: { e.preventDefault() const item = current() - if (!item || item.field.options) return + if (!item || !valid()) return + const value = answer(item.field) - if (!valid()) return + if (value !== undefined) setFormStore("value", item.field.key, value) await next(item.index, formStore.value) } @@ -730,6 +761,15 @@ function ProviderConnection(props: { return field }) + const custom = () => { + const field = select() + + if (!field?.custom) return false + const value = answer(field) + + return formStore.customOn || (value !== undefined && !field.options?.some((option) => option.value === value)) + } + return (
@@ -738,7 +778,7 @@ function ProviderConnection(props: { type="text" label={text()?.title ?? ""} placeholder={text()?.placeholder} - value={text() ? (formStore.value[text()!.key] ?? "") : ""} + value={text() ? (answer(text()!) ?? "") : ""} onChange={(value) => { const field = text() @@ -750,6 +790,62 @@ function ProviderConnection(props: { {language.t("common.continue")} + + {(field) => ( + <> +
+
{field().title}
+ option.value === answer(field()))) + } + onChange={(value) => { + const option = field().options?.[Number(value)] + + if (option) { + if (custom()) setFormStore("customText", answer(field()) ?? "") + setFormStore("customOn", false) + setFormStore("value", field().key, option.value) + + return + } + + setFormStore("customOn", true) + setFormStore("value", field().key, formStore.customText) + }} + > + + {(option, index) => ( + + )} + + + +
+ queueMicrotask(() => input.focus())} + type="text" + label={language.t("ui.messagePart.option.typeOwnAnswer")} + hideLabel + placeholder={field().placeholder ?? language.t("ui.question.custom.placeholder")} + value={answer(field()) ?? ""} + onChange={(value) => setFormStore("value", field().key, value)} + /> +
+
+
+
+ + + )} +
{select()?.title}
@@ -758,7 +854,7 @@ function ProviderConnection(props: { class="px-3" items={select()?.options ?? []} key={(x) => x.value} - current={select()?.options?.find((x) => x.value === formStore.value[select()!.key])} + current={select()?.options?.find((x) => x.value === answer(select()!))} onSelect={(value) => { if (!value) return const field = select() @@ -876,9 +972,11 @@ function ProviderConnection(props: { let apiKey: HTMLInputElement | undefined const errorID = createUniqueId() - const [formStore, setFormStore] = createStore({ + const [formStore, setFormStore] = createStore<{ + value: string + error?: string + }>({ value: "", - error: undefined as string | undefined, }) onMount(() => { @@ -890,16 +988,16 @@ function ProviderConnection(props: { if (!(e.currentTarget instanceof HTMLFormElement)) return const value = new FormData(e.currentTarget).get("apiKey") - const apiKey = typeof value === "string" ? value : "" + const key = value instanceof File || value === null ? "" : value - if (!apiKey?.trim()) { + if (!key.trim()) { setFormStore("error", language.t("provider.connect.apiKey.required")) return } setFormStore("error", undefined) - await controller.auth.connectKey(apiKey) + await controller.auth.connectKey(key) } return ( @@ -954,9 +1052,11 @@ function ProviderConnection(props: { let codeInput: HTMLInputElement | undefined const errorID = createUniqueId() - const [formStore, setFormStore] = createStore({ + const [formStore, setFormStore] = createStore<{ + value: string + error?: string + }>({ value: "", - error: undefined as string | undefined, }) onMount(() => { @@ -968,9 +1068,9 @@ function ProviderConnection(props: { if (!(e.currentTarget instanceof HTMLFormElement)) return const value = new FormData(e.currentTarget).get("code") - const code = typeof value === "string" ? value : "" + const code = value instanceof File || value === null ? "" : value - if (!code?.trim()) { + if (!code.trim()) { setFormStore("error", language.t("provider.connect.oauth.code.required")) return diff --git a/packages/tui/src/component/dialog-integration.tsx b/packages/tui/src/component/dialog-integration.tsx index b21882eb8dd..44057f95c5d 100644 --- a/packages/tui/src/component/dialog-integration.tsx +++ b/packages/tui/src/component/dialog-integration.tsx @@ -1,4 +1,5 @@ -import { TextAttributes } from "@opentui/core" +import { ScrollBoxRenderable, TextareaRenderable, TextAttributes } from "@opentui/core" +import { useTerminalDimensions } from "@opentui/solid" import type { ConnectionInfo, IntegrationCommandConnectOutput, @@ -12,7 +13,8 @@ import type { LocationRef, } from "@opencode/client" import { openUrl } from "@opencode/util/open" -import { createEffect, createMemo, createSignal, onCleanup, onMount, Show } from "solid-js" +import { createEffect, createMemo, createSignal, For, onCleanup, onMount, Show } from "solid-js" +import { createStore } from "solid-js/store" import { useClipboard } from "../context/clipboard" import { useData } from "../context/data" import { useClient } from "../context/client" @@ -25,7 +27,15 @@ import { DialogSelect } from "../ui/dialog-select" import { Link } from "../ui/link" import { useToast } from "../ui/toast" import { errorMessage } from "../util/error" -import { formLabel, formToggleMultiselect, formValidateValue, type FormAnswerField } from "../util/form" +import { + formInitialValues, + formLabel, + formRows, + formSelected, + formToggleMultiselect, + formValidateValue, + type FormAnswerField, +} from "../util/form" const INTEGRATION_PRIORITY: Record = { "opencode-go": 0, @@ -813,6 +823,13 @@ async function selectAnswer( title: string, field: Extract, ): Promise { + if (field.type === "string" && field.custom) + return new Promise((resolve) => { + dialog.replace( + () => , + () => resolve(CANCELLED), + ) + }) const options = field.type === "boolean" ? field.default === false @@ -829,18 +846,12 @@ async function selectAnswer( value: option.value as FormValue, description: option.description, })) - const choice = await new Promise((resolve) => { + return new Promise((resolve) => { dialog.replace( () => ( - + title={formLabel(field) || title} - options={[ - ...options, - ...(field.type === "string" && field.custom - ? [{ title: "Type your own answer", value: CUSTOM as typeof CUSTOM }] - : []), - ...(!field.required ? [{ title: "Skip", value: undefined }] : []), - ]} + options={[...options, ...(!field.required ? [{ title: "Skip", value: undefined }] : [])]} current={field.type === "string" ? field.default : undefined} onSelect={(option) => resolve(option.value)} /> @@ -848,11 +859,158 @@ async function selectAnswer( () => resolve(CANCELLED), ) }) - if (choice === CUSTOM) { - if (field.type !== "string") return CANCELLED - return textAnswer(dialog, title, field, "") +} + +function StringChoiceField(props: { + field: Extract + onSubmit: (value: string | undefined) => void +}) { + const dialog = useDialog() + const theme = useTheme().surface("dialog") + const dimensions = useTerminalDimensions() + const rows = formRows(props.field) + const count = rows.length + (props.field.required ? 1 : 2) + const [store, setStore] = createStore({ + selected: formSelected(props.field, props.field.default), + editing: false, + text: formInitialValues([props.field]).custom[props.field.key] ?? "", + error: "", + }) + const [textarea, setTextarea] = createSignal() + let scroll: ScrollBoxRenderable | undefined + createEffect(() => { + const row = scroll?.getChildren()[store.selected] + if (row) scroll?.scrollChildIntoView(row.id) + }) + const fg = (index: number) => (store.selected === index ? theme.text.formfield.focused : theme.text.formfield.base) + const submit = (value: string | undefined) => { + const invalid = formValidateValue(props.field, value) + if (invalid) return setStore("error", invalid) + props.onSubmit(value) } - return choice + const select = (index: number) => { + setStore({ selected: index, error: "" }) + if (index === rows.length) return setStore("editing", true) + submit(index < rows.length ? String(rows[index].value) : undefined) + } + const back = () => setStore({ editing: false, text: textarea()?.plainText ?? store.text, error: "" }) + Keymap.createLayer(() => ({ + mode: "modal", + enabled: !store.editing, + commands: [ + { + id: "dialog.select.prev", + title: "Previous answer", + group: "Form", + run: () => setStore("selected", (store.selected + count - 1) % count), + }, + { + id: "dialog.select.next", + title: "Next answer", + group: "Form", + run: () => setStore("selected", (store.selected + 1) % count), + }, + { id: "dialog.select.submit", title: "Select answer", group: "Form", run: () => select(store.selected) }, + ], + })) + Keymap.createLayer(() => ({ + mode: "modal", + priority: 1, + target: textarea, + enabled: store.editing, + commands: [ + { + id: "dialog.prompt.submit", + title: "Submit answer", + group: "Form", + run: () => submit(textarea()?.plainText.trim() || undefined), + }, + { bind: "escape", title: "Back to answers", group: "Form", run: back }, + ], + })) + return ( + + + + {formLabel(props.field)} + + (store.editing ? back() : dialog.clear())}> + esc + + + {(description) => {description()}} + row.description).length, + Math.max(3, Math.floor(dimensions().height / 2) - 6), + )} + scrollbarOptions={{ visible: false }} + ref={(value: ScrollBoxRenderable) => { + scroll = value + }} + > + + {(row, index) => ( + select(index())}> + + {index() + 1}. {row.label} + + + {(description) => ( + + {description()} + + )} + + + )} + + select(rows.length)}> + {rows.length + 1}. + {store.text || "Type your own answer"}} + > +