From 23e06475fce11b76357a31078364c33ffc0ef916 Mon Sep 17 00:00:00 2001 From: helix-white <267227783+helix-a@users.noreply.github.com> Date: Wed, 25 Nov 2026 17:19:58 +0000 Subject: [PATCH] fix: let Default be turned off and keep hand-set provider settings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - unticking Default on an action-managed provider drops `model`, instead of leaving it set - the prefill lists the current default model first, so an unchanged save keeps a hand-set default - a hand-written `{env:…}` or `{file:…}` key reference prefills as written and is saved back verbatim, outside `providerKeys` Co-Authored-By: Claude Opus 5.5 --- README.md | 17 ++++++---- instructions.md | 6 ++-- startos/actions/configureProviders.ts | 48 ++++++++++++++++----------- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 9852191..3163967 100644 --- a/README.md +++ b/README.md @@ -166,22 +166,25 @@ warning, and status gating; what follows is what the metadata cannot express. **Settings → Providers**. - **What it changes.** For each provider in the form, an entry in the `providers` map of `main/.config/opencode/opencode.json`, and its API key under - `providerKeys` in `startos/store.json`. Entries carrying + `providerKeys` in `startos/store.json`; a key given as an `{env:…}` or + `{file:…}` reference stays in the entry as written instead. Entries carrying `package: @opencode/ai/providers/openai-compatible` are rebuilt; an ID collision with any other entry is rejected before either file changes. Every other entry, and a `model` that names none of the managed entries, is left as it was. The - `model` key is set when a provider's **Default** toggle is on, or dropped when it - names a provider this save removes. A new or changed API key - restarts the service, because the server receives it as an `OPENCODE_KEY_` + `model` key is set when a provider's **Default** toggle is on; with none on, a + `model` naming a managed entry is dropped. A new or changed API key restarts + the service, because the server receives it as an `OPENCODE_KEY_` environment variable; a change that touches no key is picked up by the running server without a restart. - **Cost.** Seconds. Nothing outside the two files above is touched, and no session, repository, or project is affected. - **Repeat safety.** The form is prefilled with the OpenAI-compatible entries and is the authority for them: leaving one out disconnects it, and an empty form - disconnects them all. A literal API key on an existing entry is prefilled and - moved into `providerKeys` on save. Providers using another package are not - listed and are not touched. + disconnects them all. The provider holding the default model opens with + **Default** on and that model listed first, so saving unchanged keeps the + default. An existing entry's literal API key or key reference is prefilled; on + save the literal moves into `providerKeys` and the reference is kept as + written. Providers using another package are not listed and are not touched. - **Outputs.** Each provider with the model IDs it serves, and the default model when one is set. diff --git a/instructions.md b/instructions.md index df66bb7..ec184a1 100644 --- a/instructions.md +++ b/instructions.md @@ -83,9 +83,9 @@ Run the action again to change an endpoint or add another provider, and delete a provider by removing its entry from the list. The API key of a provider you have already configured is filled in when the form opens, so leave it as it is unless you are replacing it. OpenAI-compatible endpoints you configured by hand are -also listed, with any literal API key filled in. Providers connected in the web -app are not affected. If an ID is already in use outside this list, choose a -different ID. +also listed, with their API key — or the `{env:…}` or `{file:…}` reference that +supplies it — filled in. Providers connected in the web app are not affected. If +an ID is already in use outside this list, choose a different ID. ### Changing the password diff --git a/startos/actions/configureProviders.ts b/startos/actions/configureProviders.ts index 78dccf8..50713f9 100644 --- a/startos/actions/configureProviders.ts +++ b/startos/actions/configureProviders.ts @@ -30,6 +30,12 @@ const defaultModelOf = (model: unknown): string | undefined => { return provider && id ? `${provider}/${id}` : undefined } +const keyReference = (providerId: string) => + `{env:${providerKeyEnvVar(providerId)}}` + +// `{env:…}` and `{file:…}` keys, which OpenCode resolves itself. +const isReference = (apiKey: string) => /^\{(?:env|file):[^}]+\}$/.test(apiKey) + const providerShape = InputSpec.of({ id: Value.text({ name: i18n('ID'), @@ -142,12 +148,14 @@ export const configureProviders = sdk.Action.withInput( // input spec inputSpec, - // prefill: the OpenAI-compatible entries; each key comes from a literal in the - // entry, or from the `startos` volume the action stored it in. + // prefill: the OpenAI-compatible entries; the action's own key references + // resolve from the `startos` volume. async ({ effects }) => { const config = await opencodeJson.read().once() const providerKeys = await storeJson.read((s) => s.providerKeys).once() - const defaultProvider = defaultModelOf(config?.model)?.split('/')[0] + const defaultModel = defaultModelOf(config?.model) ?? '' + const [defaultProvider] = defaultModel.split('/') + const defaultId = defaultModel.slice(defaultProvider.length + 1) return { providers: Object.entries(config?.providers ?? {}) @@ -156,16 +164,22 @@ export const configureProviders = sdk.Action.withInput( .map(([id, provider]) => { const settings = asRecord(provider.settings) const configuredKey = asString(settings.apiKey) + const listed = Object.keys(asRecord(provider.models)) + const isDefault = id === defaultProvider + const models = + isDefault && listed.includes(defaultId) + ? [defaultId, ...listed.filter((model) => model !== defaultId)] + : listed return { id, name: asString(provider.name) ?? id, baseURL: asString(settings.baseURL) ?? '', apiKey: - configuredKey && !/\{(?:env|file):/.test(configuredKey) + configuredKey && configuredKey !== keyReference(id) ? configuredKey : (asString(providerKeys?.[id]) ?? null), - models: Object.keys(asRecord(provider.models)), - isDefault: id === defaultProvider, + models, + isDefault, } }), } @@ -175,7 +189,7 @@ export const configureProviders = sdk.Action.withInput( async ({ effects, input }) => { const providerKeys = Object.fromEntries( input.providers - .filter((provider) => provider.apiKey) + .filter((provider) => provider.apiKey && !isReference(provider.apiKey)) .map((provider) => [provider.id, provider.apiKey]), ) const marked = input.providers.find((provider) => provider.isDefault) @@ -214,7 +228,9 @@ export const configureProviders = sdk.Action.withInput( settings.baseURL = provider.baseURL if (provider.apiKey) { - settings.apiKey = `{env:${providerKeyEnvVar(provider.id)}}` + settings.apiKey = isReference(provider.apiKey) + ? provider.apiKey + : keyReference(provider.id) } else { delete settings.apiKey } @@ -237,24 +253,18 @@ export const configureProviders = sdk.Action.withInput( } } - // `model` is left as it was unless this save sets it, or it names a provider - // this save removes. - const previousDefault = defaultModelOf(config.model) - const previousProvider = previousDefault?.split('/')[0] - shownModel = previousDefault + // A `model` naming none of the managed providers is left as found. + const previousProvider = defaultModelOf(config.model)?.split('/')[0] let model: unknown = config.model if (marked) { - const next = `${marked.id}/${marked.models[0]}` - model = next - shownModel = next + model = `${marked.id}/${marked.models[0]}` } else if ( previousProvider !== undefined && - managedIds.has(previousProvider) && - !input.providers.some((provider) => provider.id === previousProvider) + managedIds.has(previousProvider) ) { model = undefined - shownModel = undefined } + shownModel = defaultModelOf(model) await storeJson.update(effects, (current) => ({ ...current,