mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add per-model OpenAI Responses API toggle (#27683)
chatd hardcoded `WithUseResponsesAPI()`, so the provider SDK's static known-model list decided whether an OpenAI model spoke the Responses API or Chat Completions. A model absent from that list silently fell back to Chat Completions until the fantasy fork was patched. This exposes the SDK's `WithResponsesAPIFunc` hook as a per-model setting, `openai_config.use_responses_api`, stored in the existing `chat_model_configs.options` JSONB. Unset keeps the known-model list, `true` forces Responses, `false` forces Chat Completions. There is no migration. It sits in a new construction-time `openai_config` section rather than in `provider_options.openai` because it selects the API when the client is built, while `provider_options` holds per-request parameters. That placement is also load-bearing: a config setting only this field would otherwise materialize an OpenAI request-options struct and turn on provider-side response storage, since `Store` defaults to true there. Three places independently decided the transport and would silently disagree with the client actually built: | Site | Effect when it disagrees | | --- | --- | | `ModelFromConfig` | the transport being overridden | | `AcceptsFilePartMediaType` | text attachments dropped, since Responses natively accepts only images and PDFs | | `UsesResponsesOptions` | the SDK type-asserts the concrete options struct, so every OpenAI option is discarded | They share one predicate here, `chatopenai.UsesResponsesAPI`, with the override threaded to each. The rest of the stack removes that threading by resolving the transport once and carrying it. Compaction overrides and the quickgen debug model built clients without `ConfigOptions`, so they now pass it and pick up both this setting and the existing Anthropic beta headers. The toggle also makes transport-conditional option handling admin-switchable, so two hardening changes ride along. `ServiceTierFromChat` now maps every tier the codersdk enum advertises (`auto`, `default`, `flex`, `scale`, `priority`); it previously returned nil for `default` and `scale`, so flipping a model to Responses silently dropped a configured `service_tier` that the API accepts (fantasy forwards the value unchanged). And a new `TestProviderOptionsTransportParity` pins, per `provider_options.openai` field, which transport honors it, against a table in ARCHITECTURE.md, so a field honored on one transport and silently ignored on the other fails the test unless recorded as intentional. Review rounds also caught two lifecycle gaps around the new field. `isZeroChatModelCallConfig` now inspects `OpenAIConfig`, so a stored options blob whose only setting is this toggle survives into GET/list responses instead of reading as `model_config: null`; `TestIsZeroChatModelCallConfigCoversEveryField` sets each config field in isolation and fails if any field is invisible to the zero check. And the model editor's update path sends an explicit empty `model_config` when an edit clears the last field, since an omitted property preserves the stored options server-side; covered by the `EditClearingLastOptionSendsEmptyConfig` story. Azure keeps following the known-model list, because the Azure provider exposes no equivalent hook. The model editor renders Azure with the OpenAI option schema, so instead of shipping a visible but inert control, the option schema generator gains a `providers` struct tag that it emits as `visible_for_providers`. Gating uses the raw provider type rather than the alias table, so the control appears only for openai-typed providers. No hand-written frontend field: the editor renders it from the generated schema. Closes https://linear.app/codercom/issue/CODAGT-874/add-completionsresponses-api-toggle-in-model-editor > Mux prepared this PR on Mike's behalf.
This commit is contained in:
@@ -15,17 +15,18 @@ import (
|
||||
|
||||
// SchemaField describes a single form field in the generated schema.
|
||||
type SchemaField struct {
|
||||
JSONName string `json:"json_name"`
|
||||
GoName string `json:"go_name"`
|
||||
Type string `json:"type"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Label string `json:"label,omitempty"`
|
||||
Required bool `json:"required"`
|
||||
Enum []string `json:"enum,omitempty"`
|
||||
InputType string `json:"input_type"`
|
||||
Hidden bool `json:"hidden,omitempty"`
|
||||
VisibleWhen string `json:"visible_when,omitempty"`
|
||||
ConflictsWith []string `json:"conflicts_with,omitempty"`
|
||||
JSONName string `json:"json_name"`
|
||||
GoName string `json:"go_name"`
|
||||
Type string `json:"type"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Label string `json:"label,omitempty"`
|
||||
Required bool `json:"required"`
|
||||
Enum []string `json:"enum,omitempty"`
|
||||
InputType string `json:"input_type"`
|
||||
Hidden bool `json:"hidden,omitempty"`
|
||||
VisibleWhen string `json:"visible_when,omitempty"`
|
||||
ConflictsWith []string `json:"conflicts_with,omitempty"`
|
||||
VisibleForProviders []string `json:"visible_for_providers,omitempty"`
|
||||
}
|
||||
|
||||
// FieldGroup holds the fields for a struct or provider.
|
||||
@@ -55,6 +56,7 @@ func main() {
|
||||
reflect.TypeOf(codersdk.ChatModelCallConfig{}),
|
||||
"",
|
||||
map[string]bool{"ProviderOptions": true},
|
||||
nil,
|
||||
)
|
||||
if err := validateFieldReferences("general", schema.General); err != nil {
|
||||
_, _ = fmt.Fprintln(os.Stderr, err)
|
||||
@@ -76,13 +78,18 @@ func main() {
|
||||
}
|
||||
|
||||
for _, p := range providerTypes {
|
||||
schema.Providers[p.key] = extractFields(p.typ, "", nil)
|
||||
schema.Providers[p.key] = extractFields(p.typ, "", nil, nil)
|
||||
if err := validateFieldReferences(p.key, schema.Providers[p.key]); err != nil {
|
||||
_, _ = fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
if err := validateProviderScopes(schema); err != nil {
|
||||
_, _ = fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
out, err := json.MarshalIndent(schema, "", "\t")
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "marshal schema: %v\n", err)
|
||||
@@ -94,6 +101,24 @@ func main() {
|
||||
_, _ = fmt.Println(string(out))
|
||||
}
|
||||
|
||||
// validateProviderScopes rejects a providers tag naming something that is
|
||||
// neither a canonical provider nor an alias, which would silently hide the
|
||||
// field from every editor.
|
||||
func validateProviderScopes(schema Schema) error {
|
||||
for _, f := range schema.General.Fields {
|
||||
for _, provider := range f.VisibleForProviders {
|
||||
if _, ok := schema.Providers[provider]; ok {
|
||||
continue
|
||||
}
|
||||
if _, ok := schema.ProviderAliases[provider]; ok {
|
||||
continue
|
||||
}
|
||||
return xerrors.Errorf("field %q has providers entry %q referencing an unknown provider", f.JSONName, provider)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateFieldReferences(group string, fg FieldGroup) error {
|
||||
names := make(map[string]bool, len(fg.Fields))
|
||||
for _, f := range fg.Fields {
|
||||
@@ -113,10 +138,9 @@ func validateFieldReferences(group string, fg FieldGroup) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// extractFields walks the struct fields of t and returns a FieldGroup.
|
||||
// prefix is used to build dot-separated json_name values for nested
|
||||
// structs. skip lists Go field names to exclude from output.
|
||||
func extractFields(t reflect.Type, prefix string, skip map[string]bool) FieldGroup {
|
||||
// Nested fields inherit an enclosing provider scope unless they declare their
|
||||
// own.
|
||||
func extractFields(t reflect.Type, prefix string, skip map[string]bool, providers []string) FieldGroup {
|
||||
var fields []SchemaField
|
||||
|
||||
for i := range t.NumField() {
|
||||
@@ -150,6 +174,11 @@ func extractFields(t reflect.Type, prefix string, skip map[string]bool) FieldGro
|
||||
// so that entire sub-objects can be marked hidden.
|
||||
hidden := f.Tag.Get("hidden") == "true"
|
||||
|
||||
fieldProviders := providers
|
||||
if tag := f.Tag.Get("providers"); tag != "" {
|
||||
fieldProviders = strings.Split(tag, ",")
|
||||
}
|
||||
|
||||
// decimal.Decimal is an opaque numeric type used for pricing
|
||||
// precision; do not recurse into its internal struct fields.
|
||||
isDecimal := ft == reflect.TypeOf(decimal.Decimal{})
|
||||
@@ -159,7 +188,7 @@ func extractFields(t reflect.Type, prefix string, skip map[string]bool) FieldGro
|
||||
// entire struct is marked hidden, in which case emit it
|
||||
// as a single opaque field.
|
||||
if ft.Kind() == reflect.Struct && !hidden && !isDecimal {
|
||||
nested := extractFields(ft, fullJSONName, nil)
|
||||
nested := extractFields(ft, fullJSONName, nil, fieldProviders)
|
||||
fields = append(fields, nested.Fields...)
|
||||
continue
|
||||
}
|
||||
@@ -184,17 +213,18 @@ func extractFields(t reflect.Type, prefix string, skip map[string]bool) FieldGro
|
||||
inputType := inferInputType(typeName, enumValues)
|
||||
|
||||
fields = append(fields, SchemaField{
|
||||
JSONName: fullJSONName,
|
||||
GoName: goFieldPath(prefix, f.Name, t, fullJSONName),
|
||||
Type: typeName,
|
||||
Description: description,
|
||||
Label: label,
|
||||
Required: required,
|
||||
Enum: enumValues,
|
||||
InputType: inputType,
|
||||
Hidden: hidden,
|
||||
VisibleWhen: visibleWhen,
|
||||
ConflictsWith: conflictsWith,
|
||||
JSONName: fullJSONName,
|
||||
GoName: goFieldPath(prefix, f.Name, t, fullJSONName),
|
||||
Type: typeName,
|
||||
Description: description,
|
||||
Label: label,
|
||||
Required: required,
|
||||
Enum: enumValues,
|
||||
InputType: inputType,
|
||||
Hidden: hidden,
|
||||
VisibleWhen: visibleWhen,
|
||||
ConflictsWith: conflictsWith,
|
||||
VisibleForProviders: fieldProviders,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user