mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
refactor: rename Ai* database identifiers to AI* (AIGOV-369) (#26327)
Adds `ai` to sqlc's `gen.go.initialisms` in `coderd/database/sqlc.yaml` so the generated DB code follows Go's initialism convention. Adds the matching `ai` -> `AI` case to the dbgen PascalCase helper (`scripts/dbgen/main.go`) so the corresponding `dbmem` / mock identifiers stay in sync. `make gen` regenerates the rest; hand-written call sites that consume DB-generated identifiers (`enterprise/audit/table.go`, `coderd/database/modelmethods.go`, `enterprise/coderd/aigatewaykeys.go`, `coderd/database/dbauthz/*`, etc.) are updated to match. Scope is deliberately limited to the database layer: - `coderd/rbac/*` (resource and scope generators) is untouched — `ResourceAi*` / `ScopeAi*` constants stay on main's casing. - `codersdk/*` (Go SDK) is untouched — `codersdk.ResourceAi*` / `codersdk.APIKeyScopeAi*` constants stay on main's casing, so external Go SDK consumers see no source-level break. - `Aibridge*` identifiers (one SQL token `aibridge`, not `ai_bridge`) are out of scope. On-the-wire values are unchanged: enum strings, RBAC resource type strings, API key scope strings, and JSON tags all stay the same. The HTTP/JSON surface is unaffected. Refs: [AIGOV-369](https://linear.app/codercom/issue/AIGOV-369/change-ai-references-in-coderddatabasemodelsgo-to-ai) 🤖 Generated with [Coder Agents](https://coder.com)
This commit is contained in:
+10
-10
@@ -235,12 +235,12 @@ func buildAIProviderFromRow(
|
||||
// OpenAI-compatible endpoints. Bedrock routes through the Anthropic
|
||||
// provider with a Bedrock discriminator in Settings.
|
||||
switch row.Type {
|
||||
case database.AiProviderTypeOpenai,
|
||||
database.AiProviderTypeAzure,
|
||||
database.AiProviderTypeGoogle,
|
||||
database.AiProviderTypeOpenaiCompat,
|
||||
database.AiProviderTypeOpenrouter,
|
||||
database.AiProviderTypeVercel:
|
||||
case database.AIProviderTypeOpenai,
|
||||
database.AIProviderTypeAzure,
|
||||
database.AIProviderTypeGoogle,
|
||||
database.AIProviderTypeOpenaiCompat,
|
||||
database.AIProviderTypeOpenrouter,
|
||||
database.AIProviderTypeVercel:
|
||||
if len(keys) == 0 && !cfg.AllowBYOK.Value() {
|
||||
return nil, xerrors.Errorf("%s provider has no api keys configured and BYOK is not enabled", row.Type)
|
||||
}
|
||||
@@ -261,13 +261,13 @@ func buildAIProviderFromRow(
|
||||
SendActorHeaders: sendActorHeaders,
|
||||
}), nil
|
||||
|
||||
case database.AiProviderTypeAnthropic, database.AiProviderTypeBedrock:
|
||||
case database.AIProviderTypeAnthropic, database.AIProviderTypeBedrock:
|
||||
bedrock := bedrockConfigFromRow(row, settings)
|
||||
// A row typed 'bedrock' authenticates exclusively via settings;
|
||||
// without populated Bedrock credentials it cannot make upstream
|
||||
// calls, so refuse rather than falling back to an unsigned
|
||||
// Anthropic client.
|
||||
if row.Type == database.AiProviderTypeBedrock && bedrock == nil {
|
||||
if row.Type == database.AIProviderTypeBedrock && bedrock == nil {
|
||||
return nil, xerrors.New("bedrock provider has no bedrock credentials configured")
|
||||
}
|
||||
// Bedrock-backed Anthropic authenticates via AWS credentials in
|
||||
@@ -293,7 +293,7 @@ func buildAIProviderFromRow(
|
||||
SendActorHeaders: sendActorHeaders,
|
||||
}, bedrock), nil
|
||||
|
||||
case database.AiProviderTypeCopilot:
|
||||
case database.AIProviderTypeCopilot:
|
||||
// Copilot is always BYOK; the per-user token is supplied on each
|
||||
// request via the Authorization header, so no keypool is built.
|
||||
return aibridge.NewCopilotProvider(aibridge.CopilotConfig{
|
||||
@@ -310,7 +310,7 @@ func buildAIProviderFromRow(
|
||||
|
||||
// disabledProviderFromRow builds a Provider stub for a disabled row.
|
||||
// Using provider.DisabledStub rather than a concrete provider avoids
|
||||
// duplicating the row.Type switch and ensures that a new AiProviderType
|
||||
// duplicating the row.Type switch and ensures that a new AIProviderType
|
||||
// value is automatically handled without requiring a matching case here.
|
||||
func disabledProviderFromRow(row database.AIProvider) (aibridge.Provider, error) {
|
||||
return aibridge.NewDisabledProviderStub(row.Name, string(row.Type)), nil
|
||||
|
||||
@@ -239,7 +239,7 @@ func TestBuildProviders(t *testing.T) {
|
||||
t.Run("NativeAnthropicDefaultBaseURL", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
row := database.AIProvider{
|
||||
Type: database.AiProviderTypeAnthropic,
|
||||
Type: database.AIProviderTypeAnthropic,
|
||||
Name: aibridge.ProviderAnthropic,
|
||||
BaseUrl: "https://api.anthropic.com/",
|
||||
}
|
||||
@@ -249,7 +249,7 @@ func TestBuildProviders(t *testing.T) {
|
||||
t.Run("NativeAnthropicCustomBaseURL", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
row := database.AIProvider{
|
||||
Type: database.AiProviderTypeAnthropic,
|
||||
Type: database.AIProviderTypeAnthropic,
|
||||
Name: "anthropic-proxy",
|
||||
BaseUrl: "https://internal-proxy.example.com/anthropic/",
|
||||
}
|
||||
@@ -263,7 +263,7 @@ func TestBuildProviders(t *testing.T) {
|
||||
model := "anthropic.claude-3-5-sonnet-20241022-v2:0"
|
||||
smallModel := "anthropic.claude-3-5-haiku-20241022-v1:0"
|
||||
row := database.AIProvider{
|
||||
Type: database.AiProviderTypeAnthropic,
|
||||
Type: database.AIProviderTypeAnthropic,
|
||||
Name: "anthropic-bedrock",
|
||||
BaseUrl: "https://bedrock-runtime.us-west-2.amazonaws.com/",
|
||||
}
|
||||
@@ -292,7 +292,7 @@ func TestBuildProviders(t *testing.T) {
|
||||
// produce a Bedrock config; the provider's generic BaseUrl is
|
||||
// not a Bedrock detection signal.
|
||||
row := database.AIProvider{
|
||||
Type: database.AiProviderTypeAnthropic,
|
||||
Type: database.AIProviderTypeAnthropic,
|
||||
Name: "anthropic-empty-bedrock",
|
||||
BaseUrl: "https://api.anthropic.com/",
|
||||
}
|
||||
@@ -319,7 +319,7 @@ func TestBuildProvidersSkipsBadRows(t *testing.T) {
|
||||
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
|
||||
|
||||
dbgen.AIProvider(t, db, database.AIProvider{
|
||||
Type: database.AiProviderTypeAnthropic,
|
||||
Type: database.AIProviderTypeAnthropic,
|
||||
Name: "anthropic-broken",
|
||||
BaseUrl: "https://api.anthropic.com/",
|
||||
Settings: sql.NullString{String: "not-json", Valid: true},
|
||||
@@ -344,7 +344,7 @@ func TestBuildProvidersSkipsBadRows(t *testing.T) {
|
||||
// rows without keys when BYOK is disabled. The row must be
|
||||
// classified as error and excluded from the snapshot.
|
||||
dbgen.AIProvider(t, db, database.AIProvider{
|
||||
Type: database.AiProviderTypeAzure,
|
||||
Type: database.AIProviderTypeAzure,
|
||||
Name: "azure-openai",
|
||||
BaseUrl: "https://example.openai.azure.com/",
|
||||
})
|
||||
@@ -363,13 +363,13 @@ func TestBuildProvidersSkipsBadRows(t *testing.T) {
|
||||
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
|
||||
|
||||
dbgen.AIProvider(t, db, database.AIProvider{
|
||||
Type: database.AiProviderTypeAnthropic,
|
||||
Type: database.AIProviderTypeAnthropic,
|
||||
Name: "anthropic-broken",
|
||||
BaseUrl: "https://api.anthropic.com/",
|
||||
Settings: sql.NullString{String: "{not valid json", Valid: true},
|
||||
})
|
||||
good := dbgen.AIProvider(t, db, database.AIProvider{
|
||||
Type: database.AiProviderTypeOpenai,
|
||||
Type: database.AIProviderTypeOpenai,
|
||||
Name: "openai-good",
|
||||
BaseUrl: "https://api.openai.com/",
|
||||
})
|
||||
@@ -401,7 +401,7 @@ func TestBuildProvidersSkipsBadRows(t *testing.T) {
|
||||
{
|
||||
name: "OpenAI",
|
||||
row: database.AIProvider{
|
||||
Type: database.AiProviderTypeOpenai,
|
||||
Type: database.AIProviderTypeOpenai,
|
||||
Name: "openai-off",
|
||||
BaseUrl: "https://api.openai.com/",
|
||||
},
|
||||
@@ -412,7 +412,7 @@ func TestBuildProvidersSkipsBadRows(t *testing.T) {
|
||||
// must reach them too. No keys, no bedrock settings.
|
||||
name: "Anthropic",
|
||||
row: database.AIProvider{
|
||||
Type: database.AiProviderTypeAnthropic,
|
||||
Type: database.AIProviderTypeAnthropic,
|
||||
Name: "anthropic-off",
|
||||
BaseUrl: "https://api.anthropic.com/",
|
||||
},
|
||||
@@ -420,7 +420,7 @@ func TestBuildProvidersSkipsBadRows(t *testing.T) {
|
||||
{
|
||||
name: "Bedrock",
|
||||
row: database.AIProvider{
|
||||
Type: database.AiProviderTypeBedrock,
|
||||
Type: database.AIProviderTypeBedrock,
|
||||
Name: "bedrock-off",
|
||||
BaseUrl: "https://bedrock-runtime.us-east-1.amazonaws.com/",
|
||||
},
|
||||
|
||||
+7
-7
@@ -3074,26 +3074,26 @@ func ReadAIProvidersFromEnv(logger slog.Logger, environ []string) ([]codersdk.AI
|
||||
// BEDROCK_* fields are accepted on anthropic (mutually exclusive
|
||||
// with KEYS) and required on bedrock. Any other TYPE rejecting
|
||||
// them prevents silently-ignored credentials.
|
||||
isBedrockType := providerType == database.AiProviderTypeBedrock
|
||||
isAnthropicType := providerType == database.AiProviderTypeAnthropic
|
||||
isBedrockType := providerType == database.AIProviderTypeBedrock
|
||||
isAnthropicType := providerType == database.AIProviderTypeAnthropic
|
||||
if !isAnthropicType && !isBedrockType && isBedrock {
|
||||
return nil, xerrors.Errorf("provider %d (%s): BEDROCK_* fields are only supported with TYPE %q or %q",
|
||||
i, p.Type, database.AiProviderTypeAnthropic, database.AiProviderTypeBedrock)
|
||||
i, p.Type, database.AIProviderTypeAnthropic, database.AIProviderTypeBedrock)
|
||||
}
|
||||
|
||||
if isBedrockType && !isBedrock {
|
||||
return nil, xerrors.Errorf("provider %d (%s): TYPE %q requires BEDROCK_* fields to be configured",
|
||||
i, p.Type, database.AiProviderTypeBedrock)
|
||||
i, p.Type, database.AIProviderTypeBedrock)
|
||||
}
|
||||
|
||||
if isBedrockType && len(p.Keys) > 0 {
|
||||
return nil, xerrors.Errorf("provider %d (%s): KEY/KEYS are not supported for TYPE %q (use BEDROCK_* fields)",
|
||||
i, p.Type, database.AiProviderTypeBedrock)
|
||||
i, p.Type, database.AIProviderTypeBedrock)
|
||||
}
|
||||
|
||||
if providerType == database.AiProviderTypeCopilot && len(p.Keys) > 0 {
|
||||
if providerType == database.AIProviderTypeCopilot && len(p.Keys) > 0 {
|
||||
return nil, xerrors.Errorf("provider %d (%s): KEY/KEYS are not supported for TYPE %q",
|
||||
i, p.Type, database.AiProviderTypeCopilot)
|
||||
i, p.Type, database.AIProviderTypeCopilot)
|
||||
}
|
||||
|
||||
// An Anthropic provider authenticates either via a bearer
|
||||
|
||||
@@ -377,7 +377,7 @@ func TestReadAIProvidersFromEnv(t *testing.T) {
|
||||
},
|
||||
expected: []codersdk.AIProviderConfig{
|
||||
{
|
||||
Type: string(database.AiProviderTypeBedrock),
|
||||
Type: string(database.AIProviderTypeBedrock),
|
||||
Name: "bedrock-prod",
|
||||
BedrockRegion: "us-east-1",
|
||||
BedrockAccessKeys: []string{"AKID"},
|
||||
@@ -642,7 +642,7 @@ func TestBuildAIProviderFromRowSetsAPIDumpDir(t *testing.T) {
|
||||
name: "OpenAI",
|
||||
row: database.AIProvider{
|
||||
Enabled: true,
|
||||
Type: database.AiProviderTypeOpenai,
|
||||
Type: database.AIProviderTypeOpenai,
|
||||
Name: "openai",
|
||||
BaseUrl: "https://api.openai.com/",
|
||||
},
|
||||
@@ -652,7 +652,7 @@ func TestBuildAIProviderFromRowSetsAPIDumpDir(t *testing.T) {
|
||||
name: "Anthropic",
|
||||
row: database.AIProvider{
|
||||
Enabled: true,
|
||||
Type: database.AiProviderTypeAnthropic,
|
||||
Type: database.AIProviderTypeAnthropic,
|
||||
Name: "anthropic",
|
||||
BaseUrl: "https://api.anthropic.com/",
|
||||
},
|
||||
@@ -662,7 +662,7 @@ func TestBuildAIProviderFromRowSetsAPIDumpDir(t *testing.T) {
|
||||
name: "Copilot",
|
||||
row: database.AIProvider{
|
||||
Enabled: true,
|
||||
Type: database.AiProviderTypeCopilot,
|
||||
Type: database.AIProviderTypeCopilot,
|
||||
Name: "copilot",
|
||||
BaseUrl: "https://api.githubcopilot.com/",
|
||||
},
|
||||
@@ -672,7 +672,7 @@ func TestBuildAIProviderFromRowSetsAPIDumpDir(t *testing.T) {
|
||||
name: "Azure",
|
||||
row: database.AIProvider{
|
||||
Enabled: true,
|
||||
Type: database.AiProviderTypeAzure,
|
||||
Type: database.AIProviderTypeAzure,
|
||||
Name: "azure",
|
||||
BaseUrl: "https://example.openai.azure.com/",
|
||||
},
|
||||
@@ -682,7 +682,7 @@ func TestBuildAIProviderFromRowSetsAPIDumpDir(t *testing.T) {
|
||||
name: "Google",
|
||||
row: database.AIProvider{
|
||||
Enabled: true,
|
||||
Type: database.AiProviderTypeGoogle,
|
||||
Type: database.AIProviderTypeGoogle,
|
||||
Name: "google",
|
||||
BaseUrl: "https://generativelanguage.googleapis.com/v1beta/openai/",
|
||||
},
|
||||
@@ -692,7 +692,7 @@ func TestBuildAIProviderFromRowSetsAPIDumpDir(t *testing.T) {
|
||||
name: "OpenAICompat",
|
||||
row: database.AIProvider{
|
||||
Enabled: true,
|
||||
Type: database.AiProviderTypeOpenaiCompat,
|
||||
Type: database.AIProviderTypeOpenaiCompat,
|
||||
Name: "openai-compat",
|
||||
BaseUrl: "https://compat.example.com/v1/",
|
||||
},
|
||||
@@ -702,7 +702,7 @@ func TestBuildAIProviderFromRowSetsAPIDumpDir(t *testing.T) {
|
||||
name: "OpenRouter",
|
||||
row: database.AIProvider{
|
||||
Enabled: true,
|
||||
Type: database.AiProviderTypeOpenrouter,
|
||||
Type: database.AIProviderTypeOpenrouter,
|
||||
Name: "openrouter",
|
||||
BaseUrl: "https://openrouter.ai/api/v1/",
|
||||
},
|
||||
@@ -712,7 +712,7 @@ func TestBuildAIProviderFromRowSetsAPIDumpDir(t *testing.T) {
|
||||
name: "Vercel",
|
||||
row: database.AIProvider{
|
||||
Enabled: true,
|
||||
Type: database.AiProviderTypeVercel,
|
||||
Type: database.AIProviderTypeVercel,
|
||||
Name: "vercel",
|
||||
BaseUrl: "https://api.v0.dev/v1/",
|
||||
},
|
||||
@@ -722,7 +722,7 @@ func TestBuildAIProviderFromRowSetsAPIDumpDir(t *testing.T) {
|
||||
name: "Bedrock",
|
||||
row: database.AIProvider{
|
||||
Enabled: true,
|
||||
Type: database.AiProviderTypeBedrock,
|
||||
Type: database.AIProviderTypeBedrock,
|
||||
Name: "bedrock",
|
||||
BaseUrl: "https://bedrock-runtime.us-east-1.amazonaws.com/",
|
||||
Settings: mustMarshalSettings(codersdk.AIProviderSettings{
|
||||
@@ -757,7 +757,7 @@ func TestBuildAIProviderFromRowBedrockWithoutSettings(t *testing.T) {
|
||||
|
||||
_, err := buildAIProviderFromRow(database.AIProvider{
|
||||
Enabled: true,
|
||||
Type: database.AiProviderTypeBedrock,
|
||||
Type: database.AIProviderTypeBedrock,
|
||||
Name: "bedrock-no-settings",
|
||||
BaseUrl: "https://bedrock-runtime.us-east-1.amazonaws.com/",
|
||||
}, nil, codersdk.AIBridgeConfig{
|
||||
|
||||
Reference in New Issue
Block a user