mirror of
https://github.com/coder/coder.git
synced 2026-10-16 14:48:56 +08:00
## Summary Move chat model management and runtime availability to organization-scoped API routes. The API now identifies the organization in every model-management operation, applies chat model RBAC and ACLs within that organization, and returns only models the caller can use there. Depends on #27958. ## Problem Chat model configurations now belong to organizations, but the existing experimental management and discovery APIs are deployment-shaped. Their routes do not identify an organization, SDK methods do not accept an organization ID, and item operations address a model configuration by ID alone. That contract cannot safely expose organization-owned model configurations. It also does not provide the organization-specific provider information, authorization behavior, ACL management, or recovery guidance needed by the settings and chat clients later in the stack. ## Fix Add organization-scoped chat model endpoints under: ```text /api/experimental/organizations/{organization}/chats/models ``` The new API provides: - Organization-scoped model listing and creation. - Model get, update, and delete operations scoped by both organization and model ID. - Model ACL read and update operations. - Organization-scoped runtime availability and provider status. - Redacted provider descriptors for model-management clients. - Chat model RBAC and row ACL enforcement for writes, runtime discovery, and Chatd model selection. - Recovery guidance when a chat's organization has no usable local model. Historical chats can retain a model reference owned by another organization, but runtime selection uses an authorized local default when one is available. Foreign, inaccessible, disabled, and otherwise unusable models are not exposed as usable selections for the organization. The existing collection and availability routes remain as default-organization compatibility endpoints in this layer: ```text GET /api/experimental/chats/model-configs POST /api/experimental/chats/model-configs GET /api/experimental/chats/models ``` These compatibility routes do not provide access to models in non-default organizations. The previous unscoped item update and delete routes are removed because item operations must include the owning organization. ## Breaking change This changes the experimental HTTP API and Go SDK contracts for chat model management. Clients must migrate model-management and availability requests to the organization-scoped routes and provide an organization identifier. In the Go SDK, the following methods now require an organization ID: - `ChatModelAvailability` - `ChatModels` - `CreateChatModel` - `UpdateChatModel` - `DeleteChatModel` `ChatModels` also changes its return value from `[]ChatModel` to `OrganizationChatModelsResponse`, which includes the organization models and the redacted provider descriptors required by management clients. New item and ACL methods likewise require both the organization ID and model ID. The default-organization compatibility routes reduce the immediate impact for collection and availability callers, but they do not preserve the previous item API or SDK method signatures. Generated clients and direct consumers of the experimental API must be updated before adopting this change. _This pull request description was generated by Coder Agents._
135 lines
3.9 KiB
Go
135 lines
3.9 KiB
Go
package coderdtest
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd"
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
"github.com/coder/coder/v2/coderd/x/chatd"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatprovider"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chattest"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
const (
|
|
// TestChatProviderOpenAICompat is the default provider for chat runtime tests.
|
|
TestChatProviderOpenAICompat = "openai-compat"
|
|
// TestChatProviderAPIKey is a non-secret API key for local chat providers.
|
|
TestChatProviderAPIKey = "test-api-key"
|
|
// TestChatModelOpenAICompat is the default model for chat runtime tests.
|
|
TestChatModelOpenAICompat = "gpt-4o-mini"
|
|
)
|
|
|
|
// OpenAICompatProviderAPIKeys returns provider keys that route OpenAI-compatible
|
|
// chat calls to baseURL.
|
|
func OpenAICompatProviderAPIKeys(baseURL string) chatprovider.ProviderAPIKeys {
|
|
return chatprovider.ProviderAPIKeys{
|
|
ByProvider: map[string]string{
|
|
TestChatProviderOpenAICompat: TestChatProviderAPIKey,
|
|
},
|
|
BaseURLByProvider: map[string]string{
|
|
TestChatProviderOpenAICompat: baseURL,
|
|
},
|
|
}
|
|
}
|
|
|
|
// FakeOpenAICompatProviderAPIKeys starts a fake OpenAI-compatible provider and
|
|
// returns provider keys for coderdtest.Options.
|
|
func FakeOpenAICompatProviderAPIKeys(t testing.TB) chatprovider.ProviderAPIKeys {
|
|
t.Helper()
|
|
return OpenAICompatProviderAPIKeys(chattest.OpenAI(t))
|
|
}
|
|
|
|
// CreateOpenAICompatChatModel creates the default provider and ChatModel used
|
|
// by chat runtime tests. Tests can pass a baseURL to route chat work to a
|
|
// specific local provider. If baseURL is empty, this helper starts a fake
|
|
// OpenAI-compatible provider.
|
|
func CreateOpenAICompatChatModel(
|
|
t testing.TB,
|
|
client *codersdk.ExperimentalClient,
|
|
baseURL string,
|
|
) codersdk.ChatModel {
|
|
t.Helper()
|
|
|
|
if baseURL == "" {
|
|
baseURL = chattest.OpenAI(t)
|
|
}
|
|
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
provider, err := client.CreateAIProvider(ctx, codersdk.CreateAIProviderRequest{
|
|
Type: codersdk.AIProviderType(TestChatProviderOpenAICompat),
|
|
Name: "test-" + uuid.NewString(),
|
|
BaseURL: baseURL,
|
|
Enabled: true,
|
|
APIKeys: []string{TestChatProviderAPIKey},
|
|
})
|
|
require.NoError(t, err)
|
|
defaultOrg, err := client.OrganizationByName(ctx, codersdk.DefaultOrganization)
|
|
require.NoError(t, err)
|
|
contextLimit := int64(4096)
|
|
isDefault := true
|
|
model, err := client.CreateChatModel(ctx, defaultOrg.ID, codersdk.CreateChatModelRequest{
|
|
AIProviderID: &provider.ID,
|
|
Model: TestChatModelOpenAICompat,
|
|
ContextLimit: &contextLimit,
|
|
IsDefault: &isDefault,
|
|
})
|
|
require.NoError(t, err)
|
|
return model
|
|
}
|
|
|
|
// WaitForChatSettled waits for a chat to leave active processing and drains
|
|
// tracked chat daemon work before returning the final row.
|
|
func WaitForChatSettled(
|
|
ctx context.Context,
|
|
t testing.TB,
|
|
api *coderd.API,
|
|
chatID uuid.UUID,
|
|
) database.Chat {
|
|
t.Helper()
|
|
|
|
require.NotNil(t, api)
|
|
waitForChatTerminalState(ctx, t, api.Database, chatID)
|
|
|
|
server := api.ChatDaemonForTest()
|
|
require.NotNil(t, server)
|
|
chatd.WaitUntilIdleForTest(server)
|
|
|
|
chat, err := getChatByIDAsSystem(ctx, api.Database, chatID)
|
|
require.NoError(t, err)
|
|
return chat
|
|
}
|
|
|
|
func waitForChatTerminalState(
|
|
ctx context.Context,
|
|
t testing.TB,
|
|
db database.Store,
|
|
chatID uuid.UUID,
|
|
) {
|
|
t.Helper()
|
|
|
|
require.Eventually(t, func() bool {
|
|
chat, err := getChatByIDAsSystem(ctx, db, chatID)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return chat.Status != database.ChatStatusRunning
|
|
}, testutil.WaitLong, testutil.IntervalFast)
|
|
}
|
|
|
|
func getChatByIDAsSystem(
|
|
ctx context.Context,
|
|
db database.Store,
|
|
chatID uuid.UUID,
|
|
) (database.Chat, error) {
|
|
// Test helper needs system scope to observe chatd-owned status changes.
|
|
//nolint:gocritic
|
|
return db.GetChatByID(dbauthz.AsSystemRestricted(ctx), chatID)
|
|
}
|