mirror of
https://github.com/coder/coder.git
synced 2026-09-22 05:05:20 +08:00
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.
325 lines
9.5 KiB
Go
325 lines
9.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/shopspring/decimal"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
// 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"`
|
|
VisibleForProviders []string `json:"visible_for_providers,omitempty"`
|
|
}
|
|
|
|
// FieldGroup holds the fields for a struct or provider.
|
|
type FieldGroup struct {
|
|
Fields []SchemaField `json:"fields"`
|
|
}
|
|
|
|
// Schema is the top-level output structure.
|
|
type Schema struct {
|
|
General FieldGroup `json:"general"`
|
|
Providers map[string]FieldGroup `json:"providers"`
|
|
ProviderAliases map[string]string `json:"provider_aliases"`
|
|
}
|
|
|
|
func main() {
|
|
schema := Schema{
|
|
Providers: make(map[string]FieldGroup),
|
|
ProviderAliases: map[string]string{
|
|
"azure": "openai",
|
|
"bedrock": "anthropic",
|
|
},
|
|
}
|
|
|
|
// General options from ChatModelCallConfig, excluding
|
|
// the provider_options field which is handled separately.
|
|
schema.General = extractFields(
|
|
reflect.TypeOf(codersdk.ChatModelCallConfig{}),
|
|
"",
|
|
map[string]bool{"ProviderOptions": true},
|
|
nil,
|
|
)
|
|
if err := validateFieldReferences("general", schema.General); err != nil {
|
|
_, _ = fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Provider-specific options. Each entry maps a provider key
|
|
// to the concrete options struct used for that provider.
|
|
providerTypes := []struct {
|
|
key string
|
|
typ reflect.Type
|
|
}{
|
|
{"openai", reflect.TypeOf(codersdk.ChatModelOpenAIProviderOptions{})},
|
|
{"anthropic", reflect.TypeOf(codersdk.ChatModelAnthropicProviderOptions{})},
|
|
{"google", reflect.TypeOf(codersdk.ChatModelGoogleProviderOptions{})},
|
|
{"openaicompat", reflect.TypeOf(codersdk.ChatModelOpenAICompatProviderOptions{})},
|
|
{"openrouter", reflect.TypeOf(codersdk.ChatModelOpenRouterProviderOptions{})},
|
|
{"vercel", reflect.TypeOf(codersdk.ChatModelVercelProviderOptions{})},
|
|
}
|
|
|
|
for _, p := range providerTypes {
|
|
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)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Print the generated header and JSON body.
|
|
_, _ = fmt.Println("// Code generated by scripts/modeloptionsgen. DO NOT EDIT.")
|
|
_, _ = 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 {
|
|
names[f.JSONName] = true
|
|
}
|
|
|
|
for _, f := range fg.Fields {
|
|
if f.VisibleWhen != "" && !names[f.VisibleWhen] {
|
|
return xerrors.Errorf("field %q in group %q has visible_when=%q referencing an unknown sibling field", f.JSONName, group, f.VisibleWhen)
|
|
}
|
|
for _, sibling := range f.ConflictsWith {
|
|
if !names[sibling] {
|
|
return xerrors.Errorf("field %q in group %q has conflicts_with entry %q referencing an unknown sibling field", f.JSONName, group, sibling)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 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() {
|
|
f := t.Field(i)
|
|
|
|
if skip != nil && skip[f.Name] {
|
|
continue
|
|
}
|
|
|
|
jsonTag := f.Tag.Get("json")
|
|
if jsonTag == "" || jsonTag == "-" {
|
|
continue
|
|
}
|
|
jsonName := strings.Split(jsonTag, ",")[0]
|
|
if jsonName == "" {
|
|
continue
|
|
}
|
|
|
|
fullJSONName := jsonName
|
|
if prefix != "" {
|
|
fullJSONName = prefix + "." + jsonName
|
|
}
|
|
|
|
// Determine the underlying type, dereferencing pointers.
|
|
ft := f.Type
|
|
if ft.Kind() == reflect.Ptr {
|
|
ft = ft.Elem()
|
|
}
|
|
|
|
// Check the hidden tag before recursing into nested structs
|
|
// 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{})
|
|
|
|
// If the field is a struct (not a map), recurse to flatten
|
|
// its children using dot-separated names — unless the
|
|
// 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, fieldProviders)
|
|
fields = append(fields, nested.Fields...)
|
|
continue
|
|
}
|
|
|
|
typeName := goTypeToSchemaType(f.Type)
|
|
description := f.Tag.Get("description")
|
|
label := f.Tag.Get("label")
|
|
enumTag := f.Tag.Get("enum")
|
|
visibleWhen := f.Tag.Get("visible_when")
|
|
|
|
var conflictsWith []string
|
|
if conflictsTag := f.Tag.Get("conflicts_with"); conflictsTag != "" {
|
|
conflictsWith = strings.Split(conflictsTag, ",")
|
|
}
|
|
|
|
var enumValues []string
|
|
if enumTag != "" {
|
|
enumValues = strings.Split(enumTag, ",")
|
|
}
|
|
|
|
required := !strings.Contains(jsonTag, "omitempty")
|
|
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,
|
|
VisibleForProviders: fieldProviders,
|
|
})
|
|
}
|
|
|
|
return FieldGroup{Fields: fields}
|
|
}
|
|
|
|
// goFieldPath builds a dot-separated Go field name for nested fields.
|
|
// For top-level fields it returns just the field name. For nested
|
|
// fields it reconstructs the parent struct field name from the prefix
|
|
// by looking at the enclosing type's fields.
|
|
func goFieldPath(prefix, name string, _ reflect.Type, fullJSONName string) string {
|
|
if prefix == "" {
|
|
return name
|
|
}
|
|
// Build the Go path by walking the JSON name segments. Each
|
|
// segment maps to a struct field that we already traversed
|
|
// during recursion, so we reconstruct the path from the JSON
|
|
// parts. The parent extractFields call sets the prefix to the
|
|
// parent json name, so we can derive the Go path from the
|
|
// json segments by title-casing each part.
|
|
parts := strings.Split(fullJSONName, ".")
|
|
goNames := make([]string, 0, len(parts))
|
|
for _, p := range parts {
|
|
goNames = append(goNames, jsonSegmentToGoName(p))
|
|
}
|
|
return strings.Join(goNames, ".")
|
|
}
|
|
|
|
// jsonSegmentToGoName converts a snake_case JSON segment to a
|
|
// PascalCase Go field name using common conventions.
|
|
func jsonSegmentToGoName(seg string) string {
|
|
words := strings.Split(seg, "_")
|
|
var b strings.Builder
|
|
for _, w := range words {
|
|
if w == "" {
|
|
continue
|
|
}
|
|
// Handle common acronyms.
|
|
upper := strings.ToUpper(w)
|
|
switch upper {
|
|
case "ID", "URL", "IP", "HTTP", "JSON", "API", "UI":
|
|
_, _ = b.WriteString(upper)
|
|
default:
|
|
_, _ = b.WriteString(strings.ToUpper(w[:1]))
|
|
_, _ = b.WriteString(w[1:])
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// goTypeToSchemaType maps a Go reflect.Type to a JSON schema type
|
|
// string.
|
|
func goTypeToSchemaType(t reflect.Type) string {
|
|
// Dereference pointers.
|
|
for t.Kind() == reflect.Ptr {
|
|
t = t.Elem()
|
|
}
|
|
|
|
// decimal.Decimal represents a precise numeric value and should
|
|
// map to the "number" schema type.
|
|
if t == reflect.TypeOf(decimal.Decimal{}) {
|
|
return "number"
|
|
}
|
|
|
|
switch t.Kind() {
|
|
case reflect.String:
|
|
return "string"
|
|
case reflect.Bool:
|
|
return "boolean"
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
return "integer"
|
|
case reflect.Float32, reflect.Float64:
|
|
return "number"
|
|
case reflect.Slice:
|
|
return "array"
|
|
case reflect.Map:
|
|
return "object"
|
|
default:
|
|
return "string"
|
|
}
|
|
}
|
|
|
|
// inferInputType decides the appropriate frontend input widget for
|
|
// a field based on its schema type and enum values.
|
|
func inferInputType(typeName string, enum []string) string {
|
|
if len(enum) > 0 {
|
|
return "select"
|
|
}
|
|
switch typeName {
|
|
case "boolean":
|
|
return "select"
|
|
case "array", "object":
|
|
return "json"
|
|
default:
|
|
return "input"
|
|
}
|
|
}
|