diff --git a/coderd/x/chatd/ARCHITECTURE.md b/coderd/x/chatd/ARCHITECTURE.md index 0435b2e8d2b..e2b90f60806 100644 --- a/coderd/x/chatd/ARCHITECTURE.md +++ b/coderd/x/chatd/ARCHITECTURE.md @@ -925,6 +925,8 @@ Debug recording replaces the wrapped client and preserves the resolved transport Azure is deliberately exempt: its provider always enables the Responses API for known models and exposes no equivalent per-model hook, so the transport keeps following the known-model list for Azure. Ignoring the override there is what keeps the decisions above in agreement with the Azure client. The exemption is narrower than it appears, because chatd never builds an azure-typed provider as a fantasy azure client: `fantasyConfigForAIBridge` folds every provider type other than anthropic, bedrock, and openai into openai-compat, which always speaks Chat Completions. Bedrock is the exception within that set: its fantasy client depends on the model ID, so `anthropic.*` bedrock models fold to the Anthropic Messages client while non-anthropic bedrock models fold to the OpenAI Responses client. +For Bedrock models using the OpenAI wire format, `resolveModelCall` coerces any set `provider_options.openai.reasoning_summary` value to `auto` before building per-call provider options. Mantle rejects `concise` and `detailed` with HTTP 400 and only accepts `auto`, currently without returning summaries. Coercion keeps the stored config portable between Bedrock and direct OpenAI and allows summaries to flow if AWS adds support later. Anthropic models on Bedrock and non-Bedrock providers are unchanged. + Both transports read the same `provider_options.openai` config, but not every field applies to both wire formats. The table below records, per field, which transport honors it; `TestProviderOptionsTransportParity` fails when a field is honored on one transport and silently ignored on the other without being recorded there as intentional. | `provider_options.openai` field | Responses | Chat Completions | diff --git a/coderd/x/chatd/model_routing_aibridge.go b/coderd/x/chatd/model_routing_aibridge.go index 6197ee485b4..0e9abd561eb 100644 --- a/coderd/x/chatd/model_routing_aibridge.go +++ b/coderd/x/chatd/model_routing_aibridge.go @@ -189,6 +189,25 @@ func (p *Server) newModel( ) } +func coerceBedrockReasoningSummary(providerType database.AIProviderType, model string, callConfig codersdk.ChatModelCallConfig) codersdk.ChatModelCallConfig { + if providerType != database.AIProviderTypeBedrock || bedrockIsAnthropicModel(model) || + callConfig.ProviderOptions == nil || callConfig.ProviderOptions.OpenAI == nil || + callConfig.ProviderOptions.OpenAI.ReasoningSummary == nil || + *callConfig.ProviderOptions.OpenAI.ReasoningSummary == "auto" { + return callConfig + } + + // Mantle rejects concise/detailed with HTTP 400 and only accepts auto. + // Coercing rather than dropping keeps configs portable to direct OpenAI + // and allows summaries if AWS adds support later. + providerOptions := *callConfig.ProviderOptions + openAI := *providerOptions.OpenAI + openAI.ReasoningSummary = new("auto") + providerOptions.OpenAI = &openAI + callConfig.ProviderOptions = &providerOptions + return callConfig +} + func parseModelConfigOptions(configOptions json.RawMessage) (codersdk.ChatModelCallConfig, error) { var callConfig codersdk.ChatModelCallConfig if len(configOptions) == 0 { diff --git a/coderd/x/chatd/model_routing_internal_test.go b/coderd/x/chatd/model_routing_internal_test.go index 901374cb3d6..e7f0a883620 100644 --- a/coderd/x/chatd/model_routing_internal_test.go +++ b/coderd/x/chatd/model_routing_internal_test.go @@ -17,6 +17,7 @@ import ( "go.uber.org/mock/gomock" "golang.org/x/xerrors" + "cdr.dev/slog/v3/sloggers/slogtest" "github.com/coder/coder/v2/coderd/aibridge" "github.com/coder/coder/v2/coderd/database" "github.com/coder/coder/v2/coderd/database/dbmock" @@ -458,6 +459,138 @@ func TestAIGatewayModelBedrockInferredOpenAIDefaultsToResponses(t *testing.T) { require.Equal(t, "/v1/responses", <-paths) } +func TestBedrockReasoningSummary(t *testing.T) { + t.Parallel() + + for _, summary := range []*string{nil, new("auto"), new("concise"), new("detailed"), new(""), new("other")} { + name := "Unset" + if summary != nil { + name = "Set_" + *summary + } + t.Run(name, func(t *testing.T) { + t.Parallel() + config := codersdk.ChatModelCallConfig{ + Temperature: new(0.5), + ProviderOptions: &codersdk.ChatModelProviderOptions{ + OpenAI: &codersdk.ChatModelOpenAIProviderOptions{ + ReasoningSummary: summary, + User: new("test-user"), + }, + Anthropic: &codersdk.ChatModelAnthropicProviderOptions{SendReasoning: new(true)}, + }, + } + before, err := json.Marshal(config) + require.NoError(t, err) + got := coerceBedrockReasoningSummary(database.AIProviderTypeBedrock, "openai.gpt-5.6-luna", config) + after, err := json.Marshal(config) + require.NoError(t, err) + require.JSONEq(t, string(before), string(after), "input config must not be mutated") + require.Equal(t, config.Temperature, got.Temperature) + require.Equal(t, config.ProviderOptions.OpenAI.User, got.ProviderOptions.OpenAI.User) + require.Same(t, config.ProviderOptions.Anthropic, got.ProviderOptions.Anthropic) + if summary == nil || *summary == "auto" { + require.Same(t, config.ProviderOptions, got.ProviderOptions) + } else { + require.NotSame(t, config.ProviderOptions, got.ProviderOptions) + require.NotSame(t, config.ProviderOptions.OpenAI, got.ProviderOptions.OpenAI) + require.Equal(t, "auto", *got.ProviderOptions.OpenAI.ReasoningSummary) + } + }) + } + + for _, config := range []codersdk.ChatModelCallConfig{{}, {ProviderOptions: &codersdk.ChatModelProviderOptions{}}} { + got := coerceBedrockReasoningSummary(database.AIProviderTypeBedrock, "openai.gpt-5.6-luna", config) + require.Equal(t, config, got) + } +} + +func TestAIGatewayModelBedrockReasoningSummary(t *testing.T) { + t.Parallel() + + for _, tt := range []struct { + name string + providerType database.AIProviderType + model string + wantSummary string + wantPath string + }{ + {name: "BedrockOpenAI", providerType: database.AIProviderTypeBedrock, model: "openai.gpt-5.6-luna", wantSummary: "auto", wantPath: "/v1/responses"}, + {name: "BedrockAnthropic", providerType: database.AIProviderTypeBedrock, model: "anthropic.claude-sonnet-4", wantSummary: "detailed", wantPath: "/v1/messages"}, + {name: "OpenAI", providerType: database.AIProviderTypeOpenai, model: "openai.gpt-5.6-luna", wantSummary: "detailed", wantPath: "/v1/responses"}, + } { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + requests := make(chan *http.Request, 1) + factory := &aibridgeTestFactory{rt: roundTripFunc(func(req *http.Request) (*http.Response, error) { + requests <- req + body := `{"id":"resp_test","object":"response","created_at":0,"status":"completed","output":[{"id":"msg_test","type":"message","role":"assistant","content":[{"type":"output_text","text":"hello"}]}],"usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}}` + if tt.wantPath == "/v1/messages" { + body = `{"id":"msg_test","type":"message","role":"assistant","content":[{"type":"text","text":"hello"}],"stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":1}}` + } + return &http.Response{ + StatusCode: http.StatusOK, + Header: http.Header{"Content-Type": []string{"application/json"}}, + Body: io.NopCloser(strings.NewReader(body)), + Request: req, + }, nil + })} + provider := aibridgeTestAIProvider(uuid.New(), "test-provider", tt.providerType) + db := dbmock.NewMockStore(gomock.NewController(t)) + db.EXPECT().GetAIProviderByID(gomock.Any(), provider.ID).Return(provider, nil) + server := &Server{ + db: db, + logger: slogtest.Make(t, nil), + aibridgeTransportFactory: aibridgeTestFactoryPointer(factory), + } + config := codersdk.ChatModelCallConfig{ + MaxOutputTokens: new(int64(64)), + ProviderOptions: &codersdk.ChatModelProviderOptions{ + OpenAI: &codersdk.ChatModelOpenAIProviderOptions{ReasoningSummary: new("detailed")}, + }, + } + if tt.providerType == database.AIProviderTypeOpenai { + config.OpenAIConfig = &codersdk.ChatModelOpenAIConfig{UseResponsesAPI: new(true)} + } + options, err := json.Marshal(config) + require.NoError(t, err) + resolved, err := server.resolveModelCall(t.Context(), modelCallSpec{ + chat: database.Chat{ID: uuid.New(), OwnerID: uuid.New()}, + explicitConfig: &database.ChatModelConfig{ + ID: uuid.New(), + Model: tt.model, + AIProviderID: uuid.NullUUID{UUID: provider.ID, Valid: true}, + Options: options, + }, + buildOptions: modelBuildOptions{ActiveAPIKeyID: uuid.NewString()}, + }) + require.NoError(t, err) + call := resolved.newCall() + call.Prompt = []fantasy.Message{{ + Role: fantasy.MessageRoleUser, + Content: []fantasy.MessagePart{fantasy.TextPart{Text: "hello"}}, + }} + _, err = resolved.model.LanguageModel().Generate(t.Context(), call) + require.NoError(t, err) + request := <-requests + require.Equal(t, tt.wantPath, request.URL.Path) + var body struct { + Reasoning *struct { + Summary string `json:"summary"` + } `json:"reasoning"` + } + require.NoError(t, json.NewDecoder(request.Body).Decode(&body)) + if tt.wantPath == "/v1/messages" { + require.Nil(t, body.Reasoning) + require.Equal(t, config.ProviderOptions, resolved.callConfig.ProviderOptions) + } else { + require.NotNil(t, body.Reasoning) + require.Equal(t, tt.wantSummary, body.Reasoning.Summary) + } + require.Equal(t, tt.wantSummary, *resolved.callConfig.ProviderOptions.OpenAI.ReasoningSummary) + }) + } +} + func TestAIGatewayModelAppliesResponsesAPIOverride(t *testing.T) { t.Parallel() diff --git a/coderd/x/chatd/modelcall.go b/coderd/x/chatd/modelcall.go index 40afd786bdd..55a28966902 100644 --- a/coderd/x/chatd/modelcall.go +++ b/coderd/x/chatd/modelcall.go @@ -167,6 +167,7 @@ func (p *Server) resolveModelCall(ctx context.Context, spec modelCallSpec) (reso } out.model = model + out.callConfig = coerceBedrockReasoningSummary(out.route.Provider.Type, modelName, out.callConfig) out.providerOptions = chatprovider.ProviderOptionsForCall(out.model, out.callConfig, spec.requestedEffort) p.logger.Debug(ctx, "resolved model call",