v0.4.10 5ac0009

discover_custom.go RawHistoryFolder

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}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close