1package provider
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9 "strings"
10 "time"
11)
12
13// Conservative metadata for models that a list endpoint describes by ID
14// only. Explicit models.json entries override these values.
15const (
16 customDiscoveryContextWindow = 32768
17 customDiscoveryMaxOutput = 4096
18)
19
20// customModelsURL derives the list endpoint from a custom provider base URL.
21// A base that already ends in an API version segment such as "/v1" gets
22// "/models" appended; a bare host gets "/v1/models".
23func customModelsURL(baseURL string) (string, error) {
24 u, err := url.Parse(strings.TrimSpace(baseURL))
25 if err != nil || u.Host == "" || (u.Scheme != "http" && u.Scheme != "https") {
26 return "", fmt.Errorf("custom provider base URL must be an http or https URL")
27 }
28 u.Path = strings.TrimRight(u.Path, "/")
29 u.RawPath = ""
30 u.RawQuery = ""
31 u.Fragment = ""
32 base := u.String()
33 if versionSegmentSuffix.MatchString(base) {
34 return base + "/models", nil
35 }
36 return base + "/v1/models", nil
37}
38
39// DiscoverCustomProvider lists models from a user-defined provider's
40// OpenAI-style /models endpoint. Only IDs are trusted from the response;
41// context and output limits use conservative defaults because
42// self-hosted gateways rarely report them consistently. Entries whose ID
43// mentions embeddings are skipped. Discovered models carry the provider's
44// base URL so they route like models.json entries.
45func DiscoverCustomProvider(ctx context.Context, providerID, baseURL, apiKey string) ([]Model, error) {
46 providerID = strings.TrimSpace(providerID)
47 if providerID == "" {
48 return nil, fmt.Errorf("custom provider discovery requires a provider id")
49 }
50 endpoint, err := customModelsURL(baseURL)
51 if err != nil {
52 return nil, err
53 }
54 req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
55 if err != nil {
56 return nil, err
57 }
58 if apiKey != "" {
59 req.Header.Set("Authorization", "Bearer "+apiKey)
60 }
61 client := &http.Client{Timeout: 3 * time.Second}
62 resp, err := client.Do(req)
63 if err != nil {
64 if ctx.Err() != nil {
65 return nil, ctx.Err()
66 }
67 return nil, fmt.Errorf("%s discovery request failed", providerID)
68 }
69 defer resp.Body.Close()
70 if resp.StatusCode != http.StatusOK {
71 return nil, fmt.Errorf("%s discovery HTTP %d", providerID, resp.StatusCode)
72 }
73 var page struct {
74 Data []struct {
75 ID string `json:"id"`
76 } `json:"data"`
77 }
78 if err := json.NewDecoder(http.MaxBytesReader(nil, resp.Body, 8<<20)).Decode(&page); err != nil {
79 return nil, fmt.Errorf("%s discovery returned invalid model data", providerID)
80 }
81 if page.Data == nil {
82 return nil, fmt.Errorf("%s discovery response is missing data", providerID)
83 }
84 var models []Model
85 seen := map[string]bool{}
86 for _, d := range page.Data {
87 id := strings.TrimSpace(d.ID)
88 if id == "" || seen[id] || strings.Contains(strings.ToLower(id), "embed") {
89 continue
90 }
91 seen[id] = true
92 models = append(models, Model{
93 Provider: providerID,
94 ID: id,
95 DisplayName: id,
96 ContextWindow: customDiscoveryContextWindow,
97 MaxOutput: customDiscoveryMaxOutput,
98 BaseURL: strings.TrimRight(strings.TrimSpace(baseURL), "/"),
99 Source: "live",
100 })
101 }
102 return models, nil
103}