Skip to content

Commit d148e00

Browse files
dannykoppingclaude
andcommitted
feat(coderd): seed ai_providers and ai_provider_keys from env at startup
Reconciles CODER_AIBRIDGE_PROVIDER_<N>_* (and the legacy single-provider env vars) with the ai_providers / ai_provider_keys tables at server startup. Runs on the AGPL startup codepath unconditionally so operators can seed providers via env without enabling the bridge or proxy features. Concurrent server starts are serialized via a Postgres advisory lock; conflicts between env and DB fail startup with a clear error. Soft-deleted rows are not resurrected; existing keys are not duplicated. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 00e8b40 commit d148e00

6 files changed

Lines changed: 812 additions & 1 deletion

File tree

cli/server.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import (
6363
"github.com/coder/coder/v2/cli/cliutil"
6464
"github.com/coder/coder/v2/cli/config"
6565
"github.com/coder/coder/v2/coderd"
66+
"github.com/coder/coder/v2/coderd/audit"
6667
"github.com/coder/coder/v2/coderd/autobuild"
6768
"github.com/coder/coder/v2/coderd/database"
6869
"github.com/coder/coder/v2/coderd/database/awsiamrds"
@@ -862,6 +863,27 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
862863
}
863864
vals.AI.BridgeConfig.Providers = append(vals.AI.BridgeConfig.Providers, aiProviders...)
864865

866+
// Reconcile env-derived AI Bridge provider configuration
867+
// with the ai_providers table. Runs unconditionally so
868+
// operators can seed providers via env without enabling
869+
// the bridge or proxy features. Concurrent server starts
870+
// are serialized via a Postgres advisory lock; conflicts
871+
// between env and DB fail startup with a clear error.
872+
//
873+
// options.Auditor is not yet populated this early in
874+
// startup, so we pass a Nop auditor; seeded providers
875+
// are still visible in the API and can be re-audited by
876+
// operator actions later.
877+
if err := coderd.SeedAIProvidersFromEnv(
878+
ctx,
879+
options.Database,
880+
vals.AI.BridgeConfig,
881+
audit.NewNop(),
882+
logger.Named("aibridge.envseed"),
883+
); err != nil {
884+
return xerrors.Errorf("seed ai providers from env: %w", err)
885+
}
886+
865887
// Manage push notifications.
866888
webpusher, err := webpush.New(ctx, ptr.Ref(options.Logger.Named("webpush")), options.Database, options.AccessURL.String())
867889
if err != nil {

0 commit comments

Comments
 (0)