Files
probo/internal/cmd/genmodels/main.go
Sacha Al Himdani 4c57d201a4 Make license declarations consistently MIT
The source headers, LICENSE files, and license metadata had drifted
apart. Align the entire project to MIT:

- Convert every source-file header to the MIT text across all comment
  styles (Go, TS, TSX, JS, MJS, SQL, CSS, GraphQL, shell), including
  SPDX-License-Identifier tags
- Set the root and cookie-banner LICENSE files to the MIT text with a
  "MIT License" title line
- Switch the package.json license fields, Docker image label, and
  cookie-banner README to MIT
- Update docs and the genmodels header generator accordingly
- Normalize copyright lines to a single format
  (Copyright (c) <year(s)> Probo Inc <hello@probo.com>.): unify the
  hello@getprobo.com and hello@probo.inc emails to hello@probo.com and
  the comma-separated years to a hyphenated range

Genuine third-party references are intentionally left untouched: the
Lucide icon attributions (Lucide is ISC) and the trivy dependency
license allowlist.

Signed-off-by: Sacha Al Himdani <sacha@probo.com>
2026-07-13 16:21:14 +02:00

216 lines
5.9 KiB
Go

// Copyright (c) 2026 Probo Inc <hello@probo.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"encoding/json"
"fmt"
"go/format"
"net/http"
"os"
"strings"
"time"
)
const openRouterURL = "https://openrouter.ai/api/v1/models"
var includedProviders = map[string]struct{}{
"anthropic": {},
"openai": {},
"google": {},
"x-ai": {},
"perplexity": {},
"amazon": {},
"meta-llama": {},
"mistralai": {},
"deepseek": {},
}
var paramFieldMap = map[string]string{
"temperature": "Temperature",
"top_p": "TopP",
"top_k": "TopK",
"frequency_penalty": "FrequencyPenalty",
"presence_penalty": "PresencePenalty",
"stop": "Stop",
"seed": "Seed",
"max_tokens": "MaxTokens",
"max_completion_tokens": "MaxTokens",
"tool_choice": "ToolChoice",
"parallel_tool_calls": "ParallelToolCalls",
"response_format": "ResponseFormat",
"structured_outputs": "StructuredOutputs",
"reasoning": "Reasoning",
"include_reasoning": "Reasoning",
}
type (
openRouterResponse struct {
Data []openRouterModel `json:"data"`
}
openRouterModel struct {
ID string `json:"id"`
Name string `json:"name"`
ContextLen int `json:"context_length"`
TopProvider struct {
MaxCompletionTokens int `json:"max_completion_tokens"`
} `json:"top_provider"`
SupportedParams []string `json:"supported_parameters"`
}
)
const mitHeader = `// Copyright (c) 2026 Probo Inc <hello@probo.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
`
func main() {
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Get(openRouterURL)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot fetch models: %v\n", err)
os.Exit(1)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
fmt.Fprintf(os.Stderr, "cannot fetch models: unexpected status %s\n", resp.Status)
os.Exit(1)
}
var result openRouterResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
fmt.Fprintf(os.Stderr, "cannot decode response: %v\n", err)
os.Exit(1)
}
var buf strings.Builder
fmt.Fprintf(
&buf,
`%s// Code generated by genmodels; DO NOT EDIT.
// Source: %s
// Generated: %s
package llm
var generatedModels = map[string]ModelDefinition{
`,
mitHeader,
openRouterURL,
time.Now().UTC().Format(time.RFC3339),
)
var count int
for _, m := range result.Data {
provider := providerFromID(m.ID)
if _, ok := includedProviders[provider]; !ok {
continue
}
fmt.Fprintf(
&buf,
` %q: {
Name: %q,
ContextLength: %d,
MaxOutputTokens: %d,
Supports: SupportedParameters{
%s },
},
`,
m.ID,
m.Name,
m.ContextLen,
m.TopProvider.MaxCompletionTokens,
buildSupports(m.SupportedParams),
)
count++
}
buf.WriteString("}\n")
src, err := format.Source([]byte(buf.String()))
if err != nil {
fmt.Fprintf(os.Stderr, "cannot format generated code: %v\n", err)
os.Exit(1)
}
if err := os.WriteFile("registry_gen.go", src, 0644); err != nil {
fmt.Fprintf(os.Stderr, "cannot write file: %v\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "genmodels: wrote %d models to registry_gen.go\n", count)
}
func providerFromID(id string) string {
provider, _, ok := strings.Cut(id, "/")
if ok {
return provider
}
return ""
}
func buildSupports(params []string) string {
fields := make(map[string]bool)
for _, p := range params {
if field, ok := paramFieldMap[p]; ok {
fields[field] = true
}
}
var buf strings.Builder
fieldNames := []string{
"Temperature", "TopP", "TopK", "FrequencyPenalty", "PresencePenalty",
"Stop", "Seed", "MaxTokens", "ToolChoice", "ParallelToolCalls",
"ResponseFormat", "StructuredOutputs", "Reasoning",
}
for _, f := range fieldNames {
fmt.Fprintf(&buf, " %s: %t,\n", f, fields[f])
}
return buf.String()
}