-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands_schema.go
More file actions
118 lines (106 loc) · 2.61 KB
/
Copy pathcommands_schema.go
File metadata and controls
118 lines (106 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
schema "github.com/modelrelay/modelrelay/providers/schema"
"github.com/spf13/cobra"
)
type schemaLintConfig struct {
provider string
toolSchema bool
}
func newSchemaCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "schema",
Short: "Schema utilities",
}
cmd.AddCommand(newSchemaLintCmd())
return cmd
}
func newSchemaLintCmd() *cobra.Command {
cfg := schemaLintConfig{}
cmd := &cobra.Command{
Use: "lint <path|->",
Short: "Validate a JSON schema",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
path := strings.TrimSpace(args[0])
if path == "" {
return errors.New("schema path is required")
}
raw, err := readSchemaInput(path)
if err != nil {
return err
}
normalized, err := schema.NormalizeJSON(raw)
if err != nil {
return err
}
if strings.TrimSpace(cfg.provider) != "" {
if err := validateSchemaForProvider(normalized, cfg); err != nil {
return err
}
}
fmt.Println("OK")
return nil
},
}
cmd.Flags().StringVar(&cfg.provider, "provider", "", "openai | anthropic | googleai | xai (optional)")
cmd.Flags().BoolVar(&cfg.toolSchema, "tool-schema", false, "Validate as tool parameters (OpenAI only)")
return cmd
}
func readSchemaInput(path string) (json.RawMessage, error) {
if path == "-" {
raw, err := io.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
if len(raw) == 0 {
return nil, errors.New("schema input is empty")
}
return json.RawMessage(raw), nil
}
raw, err := os.ReadFile(path)
if err != nil {
return nil, err
}
if len(raw) == 0 {
return nil, errors.New("schema file is empty")
}
return json.RawMessage(raw), nil
}
func validateSchemaForProvider(normalized *schema.Schema, cfg schemaLintConfig) error {
switch strings.ToLower(strings.TrimSpace(cfg.provider)) {
case "openai":
if cfg.toolSchema {
_, err := schema.AdaptOpenAIToolSchema(normalized)
return err
}
_, err := schema.AdaptOpenAI(normalized)
return err
case "anthropic":
if cfg.toolSchema {
return errors.New("tool-schema flag is not supported for anthropic")
}
_, err := schema.AdaptAnthropic(normalized)
return err
case "googleai":
if cfg.toolSchema {
return errors.New("tool-schema flag is not supported for googleai")
}
_, err := schema.AdaptGoogleAI(normalized)
return err
case "xai":
if cfg.toolSchema {
return errors.New("tool-schema flag is not supported for xai")
}
_, err := schema.AdaptXAI(normalized)
return err
default:
return fmt.Errorf("unknown provider: %s", cfg.provider)
}
}