MCPcopy Index your code
hub / github.com/coder/coder / BuildProviders

Function BuildProviders

cli/aibridged.go:117–202  ·  view source on GitHub ↗

BuildProviders loads all ai_providers rows (enabled and disabled), attaches keys to enabled rows, and constructs the equivalent [aibridge.Provider] instances. The database is the single source of truth for runtime provider configuration. Disabled rows produce a Provider stub with Enabled() == false

(ctx context.Context, db database.Store, cfg codersdk.AIBridgeConfig, logger slog.Logger)

Source from the content-addressed store, hash-verified

115// itself is propagated. This keeps a single misconfigured row from
116// taking the whole daemon down.
117func BuildProviders(ctx context.Context, db database.Store, cfg codersdk.AIBridgeConfig, logger slog.Logger) ([]aibridge.Provider, []aibridged.ProviderOutcome, error) {
118 //nolint:gocritic // AsAIBridged has a minimal permission set for this purpose.
119 authCtx := dbauthz.AsAIBridged(ctx)
120
121 var rows []database.AIProvider
122 keysByProvider := make(map[uuid.UUID][]database.AIProviderKey)
123
124 // Wrap both queries in a read-only transaction so the provider list
125 // and the key list are consistent with each other.
126 err := db.InTx(func(tx database.Store) error {
127 var err error
128 rows, err = tx.GetAIProviders(authCtx, database.GetAIProvidersParams{
129 IncludeDisabled: true,
130 })
131 if err != nil {
132 return xerrors.Errorf("load ai providers: %w", err)
133 }
134
135 if len(rows) == 0 {
136 return nil
137 }
138
139 // Load keys only for the enabled providers to avoid materializing
140 // secrets for disabled rows.
141 ids := make([]uuid.UUID, 0, len(rows))
142 for _, r := range rows {
143 if !r.Enabled {
144 continue
145 }
146 ids = append(ids, r.ID)
147 }
148 if len(ids) == 0 {
149 return nil
150 }
151 keyRows, err := tx.GetAIProviderKeysByProviderIDs(authCtx, ids)
152 if err != nil {
153 return xerrors.Errorf("load ai provider keys: %w", err)
154 }
155 for _, k := range keyRows {
156 keysByProvider[k.ProviderID] = append(keysByProvider[k.ProviderID], k)
157 }
158 return nil
159 }, &database.TxOptions{ReadOnly: true, TxIdentifier: "build_ai_providers"})
160 if err != nil {
161 return nil, nil, err
162 }
163
164 providers := make([]aibridge.Provider, 0, len(rows))
165 outcomes := make([]aibridged.ProviderOutcome, 0, len(rows))
166 enabledCount := 0
167 for _, row := range rows {
168 outcome := aibridged.ProviderOutcome{
169 Name: row.Name,
170 Type: string(row.Type),
171 }
172 if row.Enabled {
173 enabledCount++
174 }

Callers 6

startTestAIBridgeDaemonFunction · 0.92
ReloadMethod · 0.92
buildFromEnvFunction · 0.85
ReloadMethod · 0.85
ServerMethod · 0.85

Calls 8

AsAIBridgedFunction · 0.92
buildAIProviderFromRowFunction · 0.85
InTxMethod · 0.65
GetAIProvidersMethod · 0.65
EnabledMethod · 0.65
ErrorfMethod · 0.45
ErrorMethod · 0.45

Tested by 4

startTestAIBridgeDaemonFunction · 0.74
ReloadMethod · 0.74
buildFromEnvFunction · 0.68