on main help sign in
feat(schema): fix the reflection on provider options
a06fd03Kieran Klukas
internal/cmd/schema.go+35−1
@@ -4,7 +4,10 @@ import (
4 "encoding/json"
4 "encoding/json"
5 "fmt"
5 "fmt"
6
6
7+ "charm.land/catwalk/pkg/catwalk"
8+ "github.com/charmbracelet/crush/internal/agent/hyper"
7 "github.com/charmbracelet/crush/internal/config"
9 "github.com/charmbracelet/crush/internal/config"
10+ "github.com/charmbracelet/crush/internal/discover"
8 "github.com/invopop/jsonschema"
11 "github.com/invopop/jsonschema"
9 "github.com/spf13/cobra"
12 "github.com/spf13/cobra"
10)
13)
@@ -16,7 +19,9 @@ var schemaCmd = &cobra.Command{
16 Hidden: true,
19 Hidden: true,
17 RunE: func(cmd *cobra.Command, args []string) error {
20 RunE: func(cmd *cobra.Command, args []string) error {
18 reflector := new(jsonschema.Reflector)
21 reflector := new(jsonschema.Reflector)
19− bts, err := json.MarshalIndent(reflector.Reflect(&config.Config{}), "", " ")
22+ schema := reflector.Reflect(&config.Config{})
23+ setProviderTypeEnum(schema)
24+ bts, err := json.MarshalIndent(schema, "", " ")
20 if err != nil {
25 if err != nil {
21 return fmt.Errorf("failed to marshal schema: %w", err)
26 return fmt.Errorf("failed to marshal schema: %w", err)
22 }
27 }
@@ -24,3 +29,32 @@ var schemaCmd = &cobra.Command{
24 return nil
29 return nil
25 },
30 },
26}
31}
32+
33+// setProviderTypeEnum overwrites the provider `type` enum with the live set
34+// of accepted values rather than a hand-maintained struct tag. The values
35+// must match exactly what load.go validates against: the catwalk provider
36+// types, the Charm Hyper type, and any locally-discovered providers that
37+// self-register an enricher (e.g. ollama, omlx). Sourcing the enum here keeps
38+// the published schema from drifting as provider types are added or renamed.
39+func setProviderTypeEnum(schema *jsonschema.Schema) {
40+ def, ok := schema.Definitions["ProviderConfig"]
41+ if !ok || def.Properties == nil {
42+ return
43+ }
44+ typeProp, ok := def.Properties.Get("type")
45+ if !ok {
46+ return
47+ }
48+
49+ var types []string
50+ for _, t := range catwalk.KnownProviderTypes() {
51+ types = append(types, string(t))
52+ }
53+ types = append(types, string(hyper.Name))
54+ types = append(types, discover.RegisteredProviderTypes()...)
55+
56+ typeProp.Enum = make([]any, len(types))
57+ for i, t := range types {
58+ typeProp.Enum[i] = t
59+ }
60+}
internal/config/config.go+1−1
@@ -95,7 +95,7 @@ type ProviderConfig struct {
95 // The provider's API endpoint.
95 // The provider's API endpoint.
96 BaseURL string `json:"base_url,omitempty" jsonschema:"description=Base URL for the provider's API,format=uri,example=https://api.openai.com/v1"`
96 BaseURL string `json:"base_url,omitempty" jsonschema:"description=Base URL for the provider's API,format=uri,example=https://api.openai.com/v1"`
97 // The provider type, e.g. "openai", "anthropic", etc. if empty it defaults to openai.
97 // The provider type, e.g. "openai", "anthropic", etc. if empty it defaults to openai.
98− Type catwalk.Type `json:"type,omitempty" jsonschema:"description=Provider type that determines the API format,enum=openai,enum=openai-compat,enum=anthropic,enum=gemini,enum=azure,enum=vertexai,default=openai"`
98+ Type catwalk.Type `json:"type,omitempty" jsonschema:"description=Provider type that determines the API format,default=openai"`
99 // The provider's API key.
99 // The provider's API key.
100 APIKey string `json:"api_key,omitempty" jsonschema:"description=API key for authentication with the provider,example=$OPENAI_API_KEY"`
100 APIKey string `json:"api_key,omitempty" jsonschema:"description=API key for authentication with the provider,example=$OPENAI_API_KEY"`
101 // The original API key template before resolution (for re-resolution on auth errors).
101 // The original API key template before resolution (for re-resolution on auth errors).
internal/discover/enricher.go+16
@@ -2,6 +2,7 @@ package discover
2
2
3import (
3import (
4 "context"
4 "context"
5+ "sort"
5
6
6 "charm.land/catwalk/pkg/catwalk"
7 "charm.land/catwalk/pkg/catwalk"
7)
8)
@@ -42,3 +43,18 @@ func GetEnricher(providerType string) Enricher {
42func IsKnownCustomProvider(providerType string) bool {
43func IsKnownCustomProvider(providerType string) bool {
43 return enrichers[providerType] != nil
44 return enrichers[providerType] != nil
44}
45}
46+
47+// RegisteredProviderTypes returns the provider type strings that have a
48+// registered enricher, sorted for stable output. These are the custom,
49+// locally-discovered providers (e.g. ollama, omlx) that Crush accepts as
50+// a `type` value even though they are not catwalk provider types. The
51+// schema generator uses this so the published enum stays in sync with the
52+// registry instead of drifting from a hand-maintained list.
53+func RegisteredProviderTypes() []string {
54+ types := make([]string, 0, len(enrichers))
55+ for providerType := range enrichers {
56+ types = append(types, providerType)
57+ }
58+ sort.Strings(types)
59+ return types
60+}