-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathai_providers_backfill.go
More file actions
69 lines (66 loc) · 2.26 KB
/
Copy pathai_providers_backfill.go
File metadata and controls
69 lines (66 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package coderd
import (
"context"
"database/sql"
"errors"
"cdr.dev/slog/v3"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/db2sdk"
"github.com/coder/coder/v2/coderd/database/dbauthz"
)
// BackfillBedrockProviderType promotes legacy ai_providers rows stored as
// type=anthropic with Bedrock settings to type=bedrock. Must run after newAPI
// so options.Database is dbcrypt-wrapped. Idempotent; errors are logged and
// startup continues.
func BackfillBedrockProviderType(ctx context.Context, db database.Store, logger slog.Logger) {
//nolint:gocritic // Startup-only backfill; no user actor is present.
sysCtx := dbauthz.AsSystemRestricted(ctx)
providers, err := db.GetAIProviders(sysCtx, database.GetAIProvidersParams{
IncludeDeleted: false,
IncludeDisabled: true,
})
if err != nil {
logger.Error(ctx, "backfill bedrock provider type: list providers", slog.Error(err))
return
}
var promoted int
for _, provider := range providers {
if provider.Type != database.AIProviderTypeAnthropic {
continue
}
settings, err := db2sdk.AIProviderSettings(provider.Settings)
if err != nil {
logger.Warn(ctx, "backfill bedrock provider type: skip provider with unparsable settings",
slog.F("provider_id", provider.ID), slog.Error(err))
continue
}
if settings.Bedrock == nil {
continue
}
_, err = db.UpdateAIProvider(sysCtx, database.UpdateAIProviderParams{
ID: provider.ID,
Type: database.AIProviderTypeBedrock,
DisplayName: provider.DisplayName,
Icon: provider.Icon,
Enabled: provider.Enabled,
BaseUrl: provider.BaseUrl,
Settings: provider.Settings,
// SettingsKeyID is re-set by the dbcrypt wrapper on write.
SettingsKeyID: sql.NullString{},
})
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
logger.Debug(ctx, "backfill bedrock provider type: provider deleted during backfill",
slog.F("provider_id", provider.ID))
continue
}
logger.Error(ctx, "backfill bedrock provider type: provider update failed and will re-attempt on next server startup",
slog.F("provider_id", provider.ID), slog.Error(err))
continue
}
promoted++
}
if promoted > 0 {
logger.Info(ctx, "backfilled bedrock provider types", slog.F("count", promoted))
}
}