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
 57func getPricing(model Model) ModelPricing {
 58	pricing := ModelPricing{}
 59	costPrompt, err := strconv.ParseFloat(model.Pricing.Prompt, 64)
 60	if err != nil {
 61		costPrompt = 0.0
 62	}
 63	pricing.CostPer1MIn = costPrompt * 1_000_000
 64	costCompletion, err := strconv.ParseFloat(model.Pricing.Completion, 64)
 65	if err != nil {
 66		costCompletion = 0.0
 67	}
 68	pricing.CostPer1MOut = costCompletion * 1_000_000
 69
 70	costPromptCached, err := strconv.ParseFloat(model.Pricing.InputCacheWrites, 64)
 71	if err != nil {
 72		costPromptCached = 0.0
 73	}
 74	pricing.CostPer1MInCached = costPromptCached * 1_000_000
 75	costCompletionCached, err := strconv.ParseFloat(model.Pricing.InputCacheReads, 64)
 76	if err != nil {
 77		costCompletionCached = 0.0
 78	}
 79	pricing.CostPer1MOutCached = costCompletionCached * 1_000_000
 80	return pricing
 81}
 82
 83// applyModelOverrides sets supported_features for models where Synthetic
 84// omits this metadata.
 85// TODO: Remove this when they add the missing metadata.
 86func applyModelOverrides(model *Model) {
 87	switch {
 88	// All of llama support tools, none do reasoning yet
 89	case strings.HasPrefix(model.ID, "hf:meta-llama/Llama-"):
 90		model.SupportedFeatures = []string{"tools"}
 91
 92	case strings.HasPrefix(model.ID, "hf:deepseek-ai/DeepSeek-R1"):
 93		model.SupportedFeatures = []string{"tools", "reasoning"}
 94
 95	case strings.HasPrefix(model.ID, "hf:deepseek-ai/DeepSeek-V3.1"):
 96		model.SupportedFeatures = []string{"tools", "reasoning"}
 97
 98	case strings.HasPrefix(model.ID, "hf:deepseek-ai/DeepSeek-V3"):
 99		model.SupportedFeatures = []string{"tools"}
100
101	case strings.HasPrefix(model.ID, "hf:Qwen/Qwen3-235B-A22B-Thinking"):
102		model.SupportedFeatures = []string{"tools", "reasoning"}
103
104	case strings.HasPrefix(model.ID, "hf:Qwen/Qwen3-235B-A22B-Instruct"):
105		model.SupportedFeatures = []string{"tools", "reasoning"}
106
107	// The rest of Qwen3 don't support reasoning but do tools
108	case strings.HasPrefix(model.ID, "hf:Qwen/Qwen3"):
109		model.SupportedFeatures = []string{"tools"}
110
111	// Has correct metadata already, but the Kimi-K2 matcher (next) would
112	// override it to omit reasoning
113	case strings.HasPrefix(model.ID, "hf:moonshotai/Kimi-K2-Thinking"):
114		model.SupportedFeatures = []string{"tools", "reasoning"}
115
116	case strings.HasPrefix(model.ID, "hf:moonshotai/Kimi-K2"):
117		model.SupportedFeatures = []string{"tools"}
118
119	case strings.HasPrefix(model.ID, "hf:zai-org/GLM-4.5"):
120		model.SupportedFeatures = []string{"tools"}
121
122	case strings.HasPrefix(model.ID, "hf:openai/gpt-oss"):
123		model.SupportedFeatures = []string{"tools"}
124	}
125}
126
127func fetchSyntheticModels(apiEndpoint string) (*ModelsResponse, error) {
128	client := &http.Client{Timeout: 30 * time.Second}
129	req, _ := http.NewRequestWithContext(context.Background(), "GET", apiEndpoint+"/models", nil)
130	req.Header.Set("User-Agent", "Crush-Client/1.0")
131	resp, err := client.Do(req)
132	if err != nil {
133		return nil, err //nolint:wrapcheck
134	}
135	defer resp.Body.Close() //nolint:errcheck
136	if resp.StatusCode != 200 {
137		body, _ := io.ReadAll(resp.Body)
138		return nil, fmt.Errorf("status %d: %s", resp.StatusCode, body)
139	}
140	var mr ModelsResponse
141	if err := json.NewDecoder(resp.Body).Decode(&mr); err != nil {
142		return nil, err //nolint:wrapcheck
143	}
144	return &mr, nil
145}
146
147// This is used to generate the synthetic.json config file.
148func main() {
149	syntheticProvider := catwalk.Provider{
150		Name:                "Synthetic",
151		ID:                  "synthetic",
152		APIKey:              "$SYNTHETIC_API_KEY",
153		APIEndpoint:         "https://api.synthetic.new/openai/v1",
154		Type:                catwalk.TypeOpenAICompat,
155		DefaultLargeModelID: "hf:zai-org/GLM-4.6",
156		DefaultSmallModelID: "hf:deepseek-ai/DeepSeek-V3.1-Terminus",
157		Models:              []catwalk.Model{},
158	}
159
160	modelsResp, err := fetchSyntheticModels(syntheticProvider.APIEndpoint)
161	if err != nil {
162		log.Fatal("Error fetching Synthetic models:", err)
163	}
164
165	// Apply overrides for models missing supported_features metadata
166	for i := range modelsResp.Data {
167		applyModelOverrides(&modelsResp.Data[i])
168	}
169
170	for _, model := range modelsResp.Data {
171		// Skip models with small context windows
172		if model.ContextLength < 20000 {
173			continue
174		}
175
176		// Skip non-text models
177		if !slices.Contains(model.InputModalities, "text") ||
178			!slices.Contains(model.OutputModalities, "text") {
179			continue
180		}
181
182		// Ensure they support tools
183		supportsTools := slices.Contains(model.SupportedFeatures, "tools")
184		if !supportsTools {
185			continue
186		}
187
188		pricing := getPricing(model)
189		supportsImages := slices.Contains(model.InputModalities, "image")
190
191		// Check if model supports reasoning
192		canReason := slices.Contains(model.SupportedFeatures, "reasoning")
193		var reasoningLevels []string
194		var defaultReasoning string
195		if canReason {
196			reasoningLevels = []string{"low", "medium", "high"}
197			defaultReasoning = "medium"
198		}
199
200		// Strip everything before the first / for a cleaner name
201		modelName := model.Name
202		if idx := strings.Index(model.Name, "/"); idx != -1 {
203			modelName = model.Name[idx+1:]
204		}
205		// Replace hyphens with spaces
206		modelName = strings.ReplaceAll(modelName, "-", " ")
207
208		m := catwalk.Model{
209			ID:                     model.ID,
210			Name:                   modelName,
211			CostPer1MIn:            pricing.CostPer1MIn,
212			CostPer1MOut:           pricing.CostPer1MOut,
213			CostPer1MInCached:      pricing.CostPer1MInCached,
214			CostPer1MOutCached:     pricing.CostPer1MOutCached,
215			ContextWindow:          model.ContextLength,
216			CanReason:              canReason,
217			DefaultReasoningEffort: defaultReasoning,
218			ReasoningLevels:        reasoningLevels,
219			SupportsImages:         supportsImages,
220		}
221
222		// Set max tokens based on max_output_length if available, but cap at
223		// 15% of context length
224		maxFromOutput := model.MaxOutputLength / 2
225		maxAt15Pct := (model.ContextLength * 15) / 100
226		if model.MaxOutputLength > 0 && maxFromOutput <= maxAt15Pct {
227			m.DefaultMaxTokens = maxFromOutput
228		} else {
229			m.DefaultMaxTokens = model.ContextLength / 10
230		}
231
232		syntheticProvider.Models = append(syntheticProvider.Models, m)
233		fmt.Printf("Added model %s with context window %d\n",
234			model.ID, model.ContextLength)
235	}
236
237	slices.SortFunc(syntheticProvider.Models, func(a catwalk.Model, b catwalk.Model) int {
238		return strings.Compare(a.Name, b.Name)
239	})
240
241	// Save the JSON in internal/providers/configs/synthetic.json
242	data, err := json.MarshalIndent(syntheticProvider, "", "  ")
243	if err != nil {
244		log.Fatal("Error marshaling Synthetic provider:", err)
245	}
246
247	if err := os.WriteFile("internal/providers/configs/synthetic.json", data, 0o600); err != nil {
248		log.Fatal("Error writing Synthetic provider config:", err)
249	}
250
251	fmt.Printf("Generated synthetic.json with %d models\n", len(syntheticProvider.Models))
252}