Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_<ID>`
`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_<ID>`
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.

Expand Down
6 changes: 3 additions & 3 deletions instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
48 changes: 29 additions & 19 deletions startos/actions/configureProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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 ?? {})
Expand All @@ -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,
}
}),
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down