Files
coder/coderd/x/chatd/model_routing_aibridge.go
T
Cian Johnston 74d1eef834 feat: support non-Anthropic models on Bedrock Mantle (#28404)
Migrates bedrock-typed provider construction to the standalone Bedrock
provider introduced by #28420, then removes the legacy Bedrock behavior
from the Anthropic provider. Existing provider and integration tests are
updated to use the new type; legacy environment configuration preserves
the `anthropic` route name while reporting provider type `bedrock`.

Adds chatd model-ID inference for Bedrock providers: `anthropic.*`
models keep the Anthropic Messages client, while other models use the
OpenAI Responses client. BYOK header format follows the inferred wire
shape.

A credential-gated live integration test discovers the Mantle catalog
from `/v1/models` and probes every model through the bridge. The
`openai.gpt-oss-*`, `google.gemma-4-*`, and `xai.grok-*` families are
documented as currently unsupported because Mantle serves them only on
routes the prefix-based rules cannot reach.

Depends on #28420.

## Smoke tests

Validated against a local `develop.sh` server on August 22, 2026:

- Coder Agents passed with Anthropic over InvokeModel, and Anthropic,
OpenAI, and Mistral models over Mantle.
- Claude Code passed the InvokeModel and Mantle Anthropic flows.
- Crush passed all four flows, including Mantle OpenAI and third-party
Chat Completions.

Models tested: `us.anthropic.claude-sonnet-4-6`,
`anthropic.claude-sonnet-5`, `openai.gpt-5.6-luna`, and
`mistral.ministral-3-14b-instruct`.

> Generated by Coder Agents
2026-09-09 18:28:57 +01:00

411 lines
12 KiB
Go

package chatd
import (
"context"
"database/sql"
"encoding/json"
"net/http"
"strings"
fantasyanthropic "charm.land/fantasy/providers/anthropic"
fantasyopenai "charm.land/fantasy/providers/openai"
fantasyopenaicompat "charm.land/fantasy/providers/openaicompat"
"github.com/google/uuid"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/aibridge"
"github.com/coder/coder/v2/coderd/aibridged"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/x/chatd/chatdebug"
"github.com/coder/coder/v2/coderd/x/chatd/chaterror"
"github.com/coder/coder/v2/coderd/x/chatd/chatprovider"
"github.com/coder/coder/v2/codersdk"
)
const (
aibridgeLocalBaseURL = "http://coder-aibridge"
// aibridgePlaceholderAPIKey satisfies fantasy clients that require a
// non-empty API key before aibridged resolves the real credential.
aibridgePlaceholderAPIKey = "coder-aibridge"
aibridgeDelegatedBYOKMarker = "delegated"
)
type aiGatewayModelRoute struct {
Provider database.AIProvider
ModelProviderHint string
ProviderAuth aiGatewayProviderAuth
}
func newAIGatewayModelRoute(
provider database.AIProvider,
modelProviderHint string,
auth aiGatewayProviderAuth,
) aiGatewayModelRoute {
return aiGatewayModelRoute{
Provider: provider,
ModelProviderHint: modelProviderHint,
ProviderAuth: auth,
}
}
type aiGatewayProviderAuth struct {
Headers map[string]string
}
func (aiGatewayProviderAuth) String() string {
return "aiGatewayProviderAuth{Headers:<redacted>}"
}
func (a aiGatewayProviderAuth) GoString() string {
return a.String()
}
type aiGatewayRequestFormat int
const (
aiGatewayRequestFormatOpenAI aiGatewayRequestFormat = iota
aiGatewayRequestFormatAnthropic
)
type aiGatewayRoundTripper struct {
base http.RoundTripper
apiKeyID string
providerAuth aiGatewayProviderAuth
}
func (t *aiGatewayRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
ctx := aibridge.WithDelegatedAPIKeyID(req.Context(), t.apiKeyID)
cloned := req.Clone(ctx)
for name, value := range t.providerAuth.Headers {
cloned.Header.Set(name, value)
}
if len(t.providerAuth.Headers) > 0 {
cloned.Header.Set(aibridge.HeaderCoderToken, aibridgeDelegatedBYOKMarker)
}
return t.base.RoundTrip(cloned)
}
// ValidateAIGatewayProviderModel rejects slash-namespaced models when an
// OpenRouter-like gateway is configured with the OpenAI provider type.
func ValidateAIGatewayProviderModel(provider database.AIProvider, model string) error {
if provider.Type != database.AIProviderTypeOpenai {
return nil
}
if !isSlashNamespacedAIGatewayModel(model) || !isOpenRouterLikeAIGatewayProvider(provider) {
return nil
}
return xerrors.New("OpenRouter-like provider configured as type openai does not support slash-namespaced models")
}
func isSlashNamespacedAIGatewayModel(model string) bool {
prefix, suffix, ok := strings.Cut(strings.TrimSpace(model), "/")
return ok && strings.TrimSpace(prefix) != "" && strings.TrimSpace(suffix) != ""
}
func isOpenRouterLikeAIGatewayProvider(provider database.AIProvider) bool {
if strings.EqualFold(strings.TrimSpace(provider.Name), "openrouter") {
return true
}
host := aibridged.BaseURLHostname(provider.BaseUrl)
return host == "openrouter.ai" || strings.HasSuffix(host, ".openrouter.ai")
}
func (p *Server) newModel(
_ context.Context,
req modelClientRequest,
route aiGatewayModelRoute,
opts modelBuildOptions,
) (chatprovider.Model, error) {
if route.Provider.ID == uuid.Nil {
return chatprovider.Model{}, xerrors.New("AI Gateway routing requires a concrete AI provider")
}
if route.Provider.Name == "" {
return chatprovider.Model{}, xerrors.New("AI Gateway routing requires an AI provider name")
}
if opts.ActiveAPIKeyID == "" {
return chatprovider.Model{}, chaterror.WithClassification(
xerrors.New("AI Gateway routing requires the active turn API key ID"),
chaterror.ClassifiedError{
Kind: codersdk.ChatErrorKindMissingKey,
Retryable: false,
Detail: "If this error persists after resending, please report it as a bug.",
},
)
}
if err := ValidateAIGatewayProviderModel(route.Provider, req.ModelName); err != nil {
return chatprovider.Model{}, chaterror.WithClassification(
err,
chaterror.ClassifiedError{
Kind: codersdk.ChatErrorKindConfig,
Retryable: false,
Detail: "Ask an administrator to change the AI provider type to openrouter or openai-compat.",
},
)
}
factoryPtr := p.aibridgeTransportFactory
if factoryPtr == nil {
return chatprovider.Model{}, xerrors.New("AI Gateway transport factory is not configured")
}
factory := factoryPtr.Load()
if factory == nil || *factory == nil {
return chatprovider.Model{}, xerrors.New("AI Gateway transport factory is not configured")
}
rt, err := (*factory).TransportFor(route.Provider.Name, aibridge.SourceAgents)
if err != nil {
return chatprovider.Model{}, xerrors.Errorf("create AI Gateway transport: %w", err)
}
baseRT := http.RoundTripper(&aiGatewayRoundTripper{
base: rt,
apiKeyID: opts.ActiveAPIKeyID,
providerAuth: route.ProviderAuth,
})
if opts.RecordHTTP {
baseRT = &chatdebug.RecordingTransport{Base: baseRT}
}
config := fantasyConfigForAIBridge(route.Provider.Type, req.ModelName)
openAIConfig := req.CallConfig.OpenAIConfig
// Force the use of the Responses API for Bedrock models inferred to the OpenAI client.
if route.Provider.Type == database.AIProviderTypeBedrock &&
config.ProviderHint == fantasyopenai.Name &&
(openAIConfig == nil || openAIConfig.UseResponsesAPI == nil) {
if openAIConfig == nil {
openAIConfig = &codersdk.ChatModelOpenAIConfig{}
}
force := true
openAIConfig.UseResponsesAPI = &force
}
extraHeaders := mergeConfigBetaHeaders(req.ExtraHeaders, config.ProviderHint, req.CallConfig)
return newLanguageModel(
config.ProviderHint,
req.ModelName,
config.Keys,
req.UserAgent,
extraHeaders,
&http.Client{Transport: baseRT},
openAIConfig,
)
}
func parseModelConfigOptions(configOptions json.RawMessage) (codersdk.ChatModelCallConfig, error) {
var callConfig codersdk.ChatModelCallConfig
if len(configOptions) == 0 {
return callConfig, nil
}
if err := json.Unmarshal(configOptions, &callConfig); err != nil {
return codersdk.ChatModelCallConfig{}, xerrors.Errorf("parse model config options: %w", err)
}
return callConfig, nil
}
// mergeConfigBetaHeaders never mutates extraHeaders; existing entries win
// over config-derived ones.
func mergeConfigBetaHeaders(
extraHeaders map[string]string,
providerHint string,
callConfig codersdk.ChatModelCallConfig,
) map[string]string {
betaHeaders := chatprovider.BetaHeadersFromCallConfig(providerHint, &callConfig)
if len(betaHeaders) == 0 {
return extraHeaders
}
merged := make(map[string]string, len(extraHeaders)+len(betaHeaders))
for name, value := range betaHeaders {
merged[name] = value
}
for name, value := range extraHeaders {
merged[name] = value
}
return merged
}
type aibridgeFantasyConfig struct {
ProviderHint string
Keys chatprovider.ProviderAPIKeys
}
// bedrockIsAnthropicModel reports whether a model ID on a bedrock provider
// uses the Anthropic Messages wire shape. InvokeModel configurations can use a
// client-facing Claude alias while remapping it to an AWS model ID upstream.
func bedrockIsAnthropicModel(model string) bool {
return strings.HasPrefix(model, "claude-") ||
strings.HasPrefix(model, "anthropic.") ||
strings.Contains(model, ".anthropic.")
}
func fantasyConfigForAIBridge(providerType database.AIProviderType, model string) aibridgeFantasyConfig {
var fantasyProvider string
baseURL := aibridgeLocalBaseURL + "/v1"
switch providerType {
case database.AIProviderTypeAnthropic:
fantasyProvider = fantasyanthropic.Name
baseURL = aibridgeLocalBaseURL
case database.AIProviderTypeBedrock:
// Bedrock Mantle serves both Anthropic Messages and OpenAI Responses
// shapes; the model ID vendor prefix picks the wire format.
if bedrockIsAnthropicModel(model) {
fantasyProvider = fantasyanthropic.Name
baseURL = aibridgeLocalBaseURL
} else {
fantasyProvider = fantasyopenai.Name
}
case database.AIProviderTypeOpenai:
fantasyProvider = fantasyopenai.Name
default:
fantasyProvider = fantasyopenaicompat.Name
}
return aibridgeFantasyConfig{
ProviderHint: fantasyProvider,
Keys: chatprovider.ProviderAPIKeys{
ByProvider: map[string]string{
fantasyProvider: aibridgePlaceholderAPIKey,
},
BaseURLByProvider: map[string]string{
fantasyProvider: baseURL,
},
},
}
}
func aiGatewayRequestFormatForProviderType(providerType database.AIProviderType, model string) aiGatewayRequestFormat {
switch providerType {
case database.AIProviderTypeAnthropic:
return aiGatewayRequestFormatAnthropic
case database.AIProviderTypeBedrock:
// The BYOK header shape must agree with the inferred wire format.
if bedrockIsAnthropicModel(model) {
return aiGatewayRequestFormatAnthropic
}
return aiGatewayRequestFormatOpenAI
default:
return aiGatewayRequestFormatOpenAI
}
}
func (p *Server) aiGatewayProviderAuthForUser(
ctx context.Context,
ownerID uuid.UUID,
provider database.AIProvider,
format aiGatewayRequestFormat,
) (aiGatewayProviderAuth, error) {
if !p.allowBYOK {
return aiGatewayProviderAuth{}, nil
}
userKey, err := p.db.GetUserAIProviderKeyByProviderID(ctx, database.GetUserAIProviderKeyByProviderIDParams{
UserID: ownerID,
AIProviderID: provider.ID,
})
if err != nil {
if xerrors.Is(err, sql.ErrNoRows) {
return aiGatewayProviderAuth{}, nil
}
return aiGatewayProviderAuth{}, xerrors.Errorf("get user AI provider key: %w", err)
}
apiKey := strings.TrimSpace(userKey.APIKey)
if apiKey == "" {
return aiGatewayProviderAuth{}, nil
}
headers := map[string]string{}
switch format {
case aiGatewayRequestFormatAnthropic:
headers["X-Api-Key"] = apiKey
default:
headers["Authorization"] = "Bearer " + apiKey
}
return aiGatewayProviderAuth{Headers: headers}, nil
}
func (p *Server) resolveAIGatewayRoute(
ctx context.Context,
ownerID uuid.UUID,
provider database.AIProvider,
modelProviderHint string,
model string,
) (aiGatewayModelRoute, error) {
auth, err := p.aiGatewayProviderAuthForUser(
ctx,
ownerID,
provider,
aiGatewayRequestFormatForProviderType(provider.Type, model),
)
if err != nil {
return aiGatewayModelRoute{}, xerrors.Errorf("resolve AI Gateway provider auth: %w", err)
}
return newAIGatewayModelRoute(provider, modelProviderHint, auth), nil
}
func (p *Server) resolveModelRouteForConfig(
ctx context.Context,
ownerID uuid.UUID,
modelConfig database.ChatModelConfig,
) (aiGatewayModelRoute, error) {
provider, err := p.gatewayProviderForConfig(ctx, modelConfig)
if err != nil {
return aiGatewayModelRoute{}, err
}
return p.resolveAIGatewayRoute(ctx, ownerID, provider, string(provider.Type), modelConfig.Model)
}
func (p *Server) resolveModelRouteForProviderType(
ctx context.Context,
ownerID uuid.UUID,
providerType string,
) (aiGatewayModelRoute, error) {
provider, err := p.aiProviderForProviderType(ctx, providerType)
if err != nil {
return aiGatewayModelRoute{}, err
}
// This path serves hardcoded computer-use defaults (openai or
// anthropic), never bedrock, so the model name is not needed for
// format inference. An empty model makes bedrock infer OpenAI format,
// which is harmless because bedrock cannot reach this path.
return p.resolveAIGatewayRoute(
ctx,
ownerID,
provider,
chatprovider.NormalizeProvider(providerType),
"",
)
}
func (p *Server) gatewayProviderForConfig(
ctx context.Context,
modelConfig database.ChatModelConfig,
) (database.AIProvider, error) {
if !modelConfig.AIProviderID.Valid {
return database.AIProvider{}, xerrors.Errorf(
"AI Gateway routing requires AI provider metadata for model config %s (%s)",
modelConfig.ID,
modelConfig.Model,
)
}
return p.enabledAIProviderByID(ctx, modelConfig.AIProviderID.UUID)
}
func (p *Server) aiProviderForProviderType(
ctx context.Context,
providerType string,
) (database.AIProvider, error) {
providers, err := p.db.GetAIProviders(ctx, database.GetAIProvidersParams{})
if err != nil {
return database.AIProvider{}, xerrors.Errorf("get enabled AI providers: %w", err)
}
normalizedProviderType := chatprovider.NormalizeProvider(providerType)
for _, provider := range providers {
if !provider.Enabled {
continue
}
if chatprovider.NormalizeProvider(string(provider.Type)) != normalizedProviderType {
continue
}
return provider, nil
}
return database.AIProvider{}, xerrors.Errorf(
"AI Gateway routing requires a usable AI provider for provider type %q",
providerType,
)
}