-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: surface duplicate AI provider hostname as warning, not error #28208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b18a82e
80bdaf1
7d3fd3e
a39d80e
a6092cf
6da9500
dd1e3b7
b732ed5
665ba60
9c004d0
22ff79d
060766b
9fdd080
4e7620a
15704b8
47c74b1
9c8592c
bf946f9
611dd4c
103343d
cbd6a52
5ce1db6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1888,3 +1888,153 @@ func TestAIProvidersBedrockExternalID(t *testing.T) { | |
| require.Equal(t, externalIDReadOnlyMsg, sdkErr.Message) | ||
| }) | ||
| } | ||
|
|
||
| func TestAIProviderHostnameCollisionWarnings(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("CreateGetListReturnsWarning", func(t *testing.T) { | ||
| t.Parallel() | ||
| client := coderdtest.New(t, nil) | ||
| _ = coderdtest.CreateFirstUser(t, client) | ||
| ctx := testutil.Context(t, testutil.WaitLong) | ||
|
|
||
| //nolint:gocritic // Owner role is the audience for this endpoint. | ||
| first, err := client.CreateAIProvider(ctx, codersdk.CreateAIProviderRequest{ | ||
| Type: codersdk.AIProviderTypeOpenAI, | ||
| Name: "first", | ||
| Enabled: true, | ||
| BaseURL: "https://api.openai.com/v1", | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Nil(t, first.Status, "first provider in database order should not get a warning") | ||
|
|
||
| //nolint:gocritic // Owner role is the audience for this endpoint. | ||
| second, err := client.CreateAIProvider(ctx, codersdk.CreateAIProviderRequest{ | ||
| Type: codersdk.AIProviderTypeOpenAI, | ||
| Name: "second", | ||
| Enabled: true, | ||
| BaseURL: "https://api.openai.com/v2", | ||
| }) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, second.Status) | ||
| require.Len(t, second.Status.Warnings, 1) | ||
| require.Contains(t, second.Status.Warnings[0], `"first"`) | ||
| require.Contains(t, second.Status.Warnings[0], "AI Gateway Proxy") | ||
| require.Contains(t, second.Status.Warnings[0], "/api/v2/ai-gateway/second/...") | ||
|
Comment on lines
+1921
to
+1923
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this could probably be simplified to |
||
|
|
||
| //nolint:gocritic // Owner role is the audience for this endpoint. | ||
| got, err := client.AIProvider(ctx, second.ID.String()) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, got.Status) | ||
| require.Len(t, got.Status.Warnings, 1) | ||
|
|
||
| //nolint:gocritic // Owner role is the audience for this endpoint. | ||
| winner, err := client.AIProvider(ctx, first.ID.String()) | ||
| require.NoError(t, err) | ||
| require.Nil(t, winner.Status, "first provider in database order should not get a warning on get") | ||
|
|
||
| //nolint:gocritic // Owner role is the audience for this endpoint. | ||
| providers, err := client.AIProviders(ctx) | ||
| require.NoError(t, err) | ||
| require.Len(t, providers, 2) | ||
| var firstListed, secondListed codersdk.AIProvider | ||
| for _, p := range providers { | ||
| switch p.Name { | ||
| case "first": | ||
| firstListed = p | ||
| case "second": | ||
| secondListed = p | ||
| } | ||
| } | ||
| require.Nil(t, firstListed.Status, "first provider in database order should not get a warning in list") | ||
| require.NotNil(t, secondListed.Status) | ||
| require.Len(t, secondListed.Status.Warnings, 1) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we should assert the warning message here as well. |
||
| }) | ||
|
|
||
| t.Run("UpdateReturnsWarningOnEnable", func(t *testing.T) { | ||
| t.Parallel() | ||
| client := coderdtest.New(t, nil) | ||
| _ = coderdtest.CreateFirstUser(t, client) | ||
| ctx := testutil.Context(t, testutil.WaitLong) | ||
|
|
||
| //nolint:gocritic // Owner role is the audience for this endpoint. | ||
| _, err := client.CreateAIProvider(ctx, codersdk.CreateAIProviderRequest{ | ||
| Type: codersdk.AIProviderTypeOpenAI, | ||
| Name: "first", | ||
| Enabled: true, | ||
| BaseURL: "https://api.openai.com/v1", | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| //nolint:gocritic // Owner role is the audience for this endpoint. | ||
| second, err := client.CreateAIProvider(ctx, codersdk.CreateAIProviderRequest{ | ||
| Type: codersdk.AIProviderTypeOpenAI, | ||
| Name: "second", | ||
| Enabled: false, | ||
| BaseURL: "https://api.openai.com/v2", | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Nil(t, second.Status) | ||
|
|
||
| //nolint:gocritic // Owner role is the audience for this endpoint. | ||
| updated, err := client.UpdateAIProvider(ctx, second.ID.String(), codersdk.UpdateAIProviderRequest{ | ||
| Enabled: ptr.Ref(true), | ||
| }) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, updated.Status) | ||
| require.Len(t, updated.Status.Warnings, 1) | ||
| require.Contains(t, updated.Status.Warnings[0], `"first"`) | ||
| require.Contains(t, updated.Status.Warnings[0], "AI Gateway Proxy") | ||
| require.Contains(t, updated.Status.Warnings[0], "/api/v2/ai-gateway/second/...") | ||
| }) | ||
|
|
||
| t.Run("NoWarningWhenNoCollision", func(t *testing.T) { | ||
| t.Parallel() | ||
| client := coderdtest.New(t, nil) | ||
| _ = coderdtest.CreateFirstUser(t, client) | ||
| ctx := testutil.Context(t, testutil.WaitLong) | ||
|
|
||
| //nolint:gocritic // Owner role is the audience for this endpoint. | ||
| first, err := client.CreateAIProvider(ctx, codersdk.CreateAIProviderRequest{ | ||
| Type: codersdk.AIProviderTypeOpenAI, | ||
| Name: "first", | ||
| Enabled: true, | ||
| BaseURL: "https://api.openai.com/v1", | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Nil(t, first.Status) | ||
|
|
||
| //nolint:gocritic // Owner role is the audience for this endpoint. | ||
| second, err := client.CreateAIProvider(ctx, codersdk.CreateAIProviderRequest{ | ||
| Type: codersdk.AIProviderTypeAnthropic, | ||
| Name: "second", | ||
| Enabled: true, | ||
| BaseURL: "https://api.anthropic.com/v1", | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Nil(t, second.Status) | ||
| }) | ||
|
|
||
| t.Run("UpdateSelfNoWarning", func(t *testing.T) { | ||
| t.Parallel() | ||
| client := coderdtest.New(t, nil) | ||
| _ = coderdtest.CreateFirstUser(t, client) | ||
| ctx := testutil.Context(t, testutil.WaitLong) | ||
|
|
||
| //nolint:gocritic // Owner role is the audience for this endpoint. | ||
| first, err := client.CreateAIProvider(ctx, codersdk.CreateAIProviderRequest{ | ||
| Type: codersdk.AIProviderTypeOpenAI, | ||
| Name: "first", | ||
| Enabled: true, | ||
| BaseURL: "https://api.openai.com/v1", | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| //nolint:gocritic // Owner role is the audience for this endpoint. | ||
| updated, err := client.UpdateAIProvider(ctx, first.ID.String(), codersdk.UpdateAIProviderRequest{ | ||
| BaseURL: ptr.Ref("https://api.openai.com/v2"), | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Nil(t, updated.Status, "update-self should not trigger a warning") | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package aibridged_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/coder/coder/v2/coderd/aibridged" | ||
| ) | ||
|
|
||
| func TestBaseURLHostname(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| baseURL string | ||
| want string | ||
| }{ | ||
| {name: "URL", baseURL: "https://openrouter.ai/api/v1", want: "openrouter.ai"}, | ||
| {name: "BareHost", baseURL: "openrouter.ai", want: "openrouter.ai"}, | ||
| {name: "HostWithPort", baseURL: "https://openrouter.ai:443/api/v1", want: "openrouter.ai"}, | ||
| {name: "UppercaseHost", baseURL: "https://API.OpenRouter.AI/v1", want: "api.openrouter.ai"}, | ||
| {name: "IPv6", baseURL: "https://[::1]:8080/v1", want: "::1"}, | ||
| {name: "Empty", baseURL: "", want: ""}, | ||
| {name: "Invalid", baseURL: "://", want: ""}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| require.Equal(t, tt.want, aibridged.BaseURLHostname(tt.baseURL)) | ||
| }) | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we send the warning even if AI Gateway proxy is disabled? We don't need to check if it is enabled, but at least we should add a note like "If AI Gateway Proxy is enabled, this provider will be excluded from proxy routing.”