main.go

  1// Package main provides a command-line tool to fetch models from Synthetic
  2// and generate a configuration file for the provider.
  3package main
  4
  5import (
  6	"context"
  7	"encoding/json"
  8	"fmt"
  9	"io"
 10	"log"
 11	"net/http"
 12	"os"
 13	"slices"
 14	"strconv"
 15	"strings"
 16	"time"
 17
 18	"github.com/charmbracelet/catwalk/pkg/catwalk"
 19)
 20
 21// Model represents a model from the Synthetic API.
 22type Model struct {
 23	ID                string   `json:"id"`
 24	Name              string   `json:"name"`
 25	InputModalities   []string `json:"input_modalities"`
 26	OutputModalities  []string `json:"output_modalities"`
 27	ContextLength     int64    `json:"context_length"`
 28	MaxOutputLength   int64    `json:"max_output_length,omitempty"`
 29	Pricing           Pricing  `json:"pricing"`
 30	SupportedFeatures []string `json:"supported_features,omitempty"`
 31}
 32
 33// Pricing contains the pricing information for different operations.
 34type Pricing struct {
 35	Prompt           string `json:"prompt"`
 36	Completion       string `json:"completion"`
 37	Image            string `json:"image"`
 38	Request          string `json:"request"`
 39	InputCacheReads  string `json:"input_cache_reads"`
 40	InputCacheWrites string `json:"input_cache_writes"`
 41}
 42
 43// ModelsResponse is the response structure for the Synthetic models API.
 44type ModelsResponse struct {
 45	Data []Model `json:"data"`
 46}
 47
 48// ModelPricing is the pricing structure for a model, detailing costs per
 49// million tokens for input and output, both cached and uncached.
 50type ModelPricing struct {
 51	CostPer1MIn        float64 `json:"cost_per_1m_in"`
 52	CostPer1MOut       float64 `json:"cost_per_1m_out"`
 53	CostPer1MInCached  float64 `json:"cost_per_1m_in_cached"`
 54	CostPer1MOutCached float64 `json:"cost_per_1m_out_cached"`
 55}
 56
 57// parsePrice extracts a float from Synthetic's price format (e.g. "$0.00000055").
 58func parsePrice(s string) float64 {
 59	s = strings.TrimPrefix(s, "$")
 60	v, err := strconv.ParseFloat(s, 64)
 61	if err != nil {
 62		return 0.0
 63	}
 64	return v
 65}
 66
 67func getPricing(model Model) ModelPricing {
 68	return ModelPricing{
 69		CostPer1MIn:        parsePrice(model.Pricing.Prompt) * 1_000_000,
 70		CostPer1MOut:       parsePrice(model.Pricing.Completion) * 1_000_000,
 71		CostPer1MInCached:  parsePrice(model.Pricing.InputCacheReads) * 1_000_000,
 72		CostPer1MOutCached: parsePrice(model.Pricing.InputCacheReads) * 1_000_000,
 73	}
 74}
 75
 76// applyModelOverrides sets supported_features for models where Synthetic
 77// omits this metadata.
 78// TODO: Remove this when they add the missing metadata.
 79func applyModelOverrides(model *Model) {
 80	switch {
 81	// All of llama support tools, none do reasoning yet
 82	case strings.HasPrefix(model.ID, "hf:meta-llama/Llama-"):
 83		model.SupportedFeatures = []string{"tools"}
 84
 85	case strings.HasPrefix(model.ID, "hf:deepseek-ai/DeepSeek-R1"):
 86		model.SupportedFeatures = []string{"tools", "reasoning"}
 87
 88	case strings.HasPrefix(model.ID, "hf:deepseek-ai/DeepSeek-V3.1"):
 89		model.SupportedFeatures = []string{"tools", "reasoning"}
 90
 91	case strings.HasPrefix(model.ID, "hf:deepseek-ai/DeepSeek-V3.2"):
 92		model.SupportedFeatures = []string{"tools", "reasoning"}
 93
 94	case strings.HasPrefix(model.ID, "hf:deepseek-ai/DeepSeek-V3"):
 95		model.SupportedFeatures = []string{"tools"}
 96
 97	case strings.HasPrefix(model.ID, "hf:Qwen/Qwen3-235B-A22B-Thinking"):
 98		model.SupportedFeatures = []string{"tools", "reasoning"}
 99
100	case strings.HasPrefix(model.ID, "hf:Qwen/Qwen3-235B-A22B-Instruct"):
101		model.SupportedFeatures = []string{"tools", "reasoning"}
102
103	// The rest of Qwen3 don't support reasoning but do tools
104	case strings.HasPrefix(model.ID, "hf:Qwen/Qwen3"):
105		model.SupportedFeatures = []string{"tools"}
106
107	// Has correct metadata already, but the Kimi-K2 matcher (next) would
108	// override it to omit reasoning
109	case strings.HasPrefix(model.ID, "hf:moonshotai/Kimi-K2-Thinking"):
110		model.SupportedFeatures = []string{"tools", "reasoning"}
111
112	case strings.HasPrefix(model.ID, "hf:moonshotai/Kimi-K2"):
113		model.SupportedFeatures = []string{"tools"}
114
115	case strings.HasPrefix(model.ID, "hf:zai-org/GLM-4.5"):
116		model.SupportedFeatures = []string{"tools"}
117
118	case strings.HasPrefix(model.ID, "hf:openai/gpt-oss"):
119		model.SupportedFeatures = []string{"tools"}
120	}
121}
122
123func fetchSyntheticModels(apiEndpoint string) (*ModelsResponse, error) {
124	client := &http.Client{Timeout: 30 * time.Second}
125	req, _ := http.NewRequestWithContext(context.Background(), "GET", apiEndpoint+"/models", nil)
126	req.Header.Set("User-Agent", "Crush-Client/1.0")
127	resp, err := client.Do(req)
128	if err != nil {
129		return nil, err //nolint:wrapcheck
130	}
131	defer resp.Body.Close() //nolint:errcheck
132	if resp.StatusCode != 200 {
133		body, _ := io.ReadAll(resp.Body)
134		return nil, fmt.Errorf("status %d: %s", resp.StatusCode, body)
135	}
136	var mr ModelsResponse
137	if err := json.NewDecoder(resp.Body).Decode(&mr); err != nil {
138		return nil, err //nolint:wrapcheck
139	}
140	return &mr, nil
141}
142
143// This is used to generate the synthetic.json config file.
144func main() {
145	syntheticProvider := catwalk.Provider{
146		Name:                "Synthetic",
147		ID:                  "synthetic",
148		APIKey:              "$SYNTHETIC_API_KEY",
149		APIEndpoint:         "https://api.synthetic.new/openai/v1",
150		Type:                catwalk.TypeOpenAICompat,
151		DefaultLargeModelID: "hf:zai-org/GLM-4.7",
152		DefaultSmallModelID: "hf:deepseek-ai/DeepSeek-V3.1-Terminus",
153		Models:              []catwalk.Model{},
154	}
155
156	modelsResp, err := fetchSyntheticModels(syntheticProvider.APIEndpoint)
157	if err != nil {
158		log.Fatal("Error fetching Synthetic models:", err)
159	}
160
161	// Apply overrides for models missing supported_features metadata
162	for i := range modelsResp.Data {
163		applyModelOverrides(&modelsResp.Data[i])
164	}
165
166	for _, model := range modelsResp.Data {
167		// Skip models with small context windows
168		if model.ContextLength < 20000 {
169			continue
170		}
171
172		// Skip non-text models
173		if !slices.Contains(model.InputModalities, "text") ||
174			!slices.Contains(model.OutputModalities, "text") {
175			continue
176		}
177
178		// Ensure they support tools
179		supportsTools := slices.Contains(model.SupportedFeatures, "tools")
180		if !supportsTools {
181			continue
182		}
183
184		pricing := getPricing(model)
185		supportsImages := slices.Contains(model.InputModalities, "image")
186
187		// Check if model supports reasoning
188		canReason := slices.Contains(model.SupportedFeatures, "reasoning")
189		var reasoningLevels []string
190		var defaultReasoning string
191		if canReason {
192			reasoningLevels = []string{"low", "medium", "high"}
193			defaultReasoning = "medium"
194		}
195
196		// Strip everything before the first / for a cleaner name
197		modelName := model.Name
198		if idx := strings.Index(model.Name, "/"); idx != -1 {
199			modelName = model.Name[idx+1:]
200		}
201		// Replace hyphens with spaces
202		modelName = strings.ReplaceAll(modelName, "-", " ")
203
204		m := catwalk.Model{
205			ID:                     model.ID,
206			Name:                   modelName,
207			CostPer1MIn:            pricing.CostPer1MIn,
208			CostPer1MOut:           pricing.CostPer1MOut,
209			CostPer1MInCached:      pricing.CostPer1MInCached,
210			CostPer1MOutCached:     pricing.CostPer1MOutCached,
211			ContextWindow:          model.ContextLength,
212			CanReason:              canReason,
213			DefaultReasoningEffort: defaultReasoning,
214			ReasoningLevels:        reasoningLevels,
215			SupportsImages:         supportsImages,
216		}
217
218		// Set max tokens based on max_output_length if available, but cap at
219		// 15% of context length
220		maxFromOutput := model.MaxOutputLength / 2
221		maxAt15Pct := (model.ContextLength * 15) / 100
222		if model.MaxOutputLength > 0 && maxFromOutput <= maxAt15Pct {
223			m.DefaultMaxTokens = maxFromOutput
224		} else {
225			m.DefaultMaxTokens = model.ContextLength / 10
226		}
227
228		syntheticProvider.Models = append(syntheticProvider.Models, m)
229		fmt.Printf("Added model %s with context window %d\n",
230			model.ID, model.ContextLength)
231	}
232
233	slices.SortFunc(syntheticProvider.Models, func(a catwalk.Model, b catwalk.Model) int {
234		return strings.Compare(a.Name, b.Name)
235	})
236
237	// Save the JSON in internal/providers/configs/synthetic.json
238	data, err := json.MarshalIndent(syntheticProvider, "", "  ")
239	if err != nil {
240		log.Fatal("Error marshaling Synthetic provider:", err)
241	}
242
243	if err := os.WriteFile("internal/providers/configs/synthetic.json", data, 0o600); err != nil {
244		log.Fatal("Error writing Synthetic provider config:", err)
245	}
246
247	fmt.Printf("Generated synthetic.json with %d models\n", len(syntheticProvider.Models))
248}