Add upstream model list fetching and a Codex-specific model section

This commit is contained in:
kate522 committed 2026-10-18 19:33:32 +08:00
1 parent d759fe5e5d
commit f3bf1472e7
12 files changed
+339 -16

No files matched your search

+27
View File
@@ -42,6 +42,33 @@ func (c *Client) endpoint() string {
return c.BaseURL + "/chat/completions"
}
// ListModels fetches the upstream model catalog. Not every provider
// implements it; callers should treat an error as "list unavailable"
// and fall back to manual entry.
func (c *Client) ListModels(ctx context.Context) (*ModelList, error) {
httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, c.BaseURL+"/models", nil)
if err != nil {
return nil, err
}
httpReq.Header.Set("Accept", "application/json")
if c.APIKey != "" {
httpReq.Header.Set("Authorization", "Bearer "+c.APIKey)
}
resp, err := c.HTTP.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, &UpstreamError{StatusCode: resp.StatusCode, Message: readErrorBody(resp)}
}
var out ModelList
if err := json.NewDecoder(io.LimitReader(resp.Body, 8<<20)).Decode(&out); err != nil {
return nil, fmt.Errorf("decode upstream model list: %w", err)
}
return &out, nil
}
func (c *Client) do(ctx context.Context, req *Request) (*http.Response, error) {
body, err := json.Marshal(req)
if err != nil {