fetchProviderModels fetches the model list from a provider's own /v1/models endpoint. Only works for alias providers with a predefined BaseURL. The request: - Uses httpclient.NewHTTPClient so it carries the Cagent User-Agent and OpenTelemetry tracing like every other outbound call. - Sends Bearer a
(ctx context.Context, providerName string, env environment.Provider)
| 284 | // provider that requires the API key on /v1/models. |
| 285 | // - Bounded by listTimeout (5s) so a slow endpoint cannot stall `models list`. |
| 286 | func fetchProviderModels(ctx context.Context, providerName string, env environment.Provider) []string { |
| 287 | alias, ok := provider.LookupAlias(providerName) |
| 288 | if !ok || alias.BaseURL == "" { |
| 289 | return nil |
| 290 | } |
| 291 | |
| 292 | modelsURL := strings.TrimRight(alias.BaseURL, "/") + "/models" |
| 293 | |
| 294 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, modelsURL, http.NoBody) |
| 295 | if err != nil { |
| 296 | slog.WarnContext(ctx, "failed to create request for provider models", "url", modelsURL, "error", err) |
| 297 | return nil |
| 298 | } |
| 299 | |
| 300 | // Send the alias's declared API key so providers that require it on |
| 301 | // /v1/models authenticate the request instead of returning 401/403. |
| 302 | if alias.TokenEnvVar != "" { |
| 303 | if token, _ := env.Get(ctx, alias.TokenEnvVar); token != "" { |
| 304 | req.Header.Set("Authorization", "Bearer "+token) |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | ctx, cancel := context.WithTimeout(ctx, listTimeout) |
| 309 | defer cancel() |
| 310 | |
| 311 | return dispatchModelsRequest(ctx, req, httpclient.NewHTTPClient(ctx)) |
| 312 | } |
| 313 | |
| 314 | // fetchModelsFromURL fetches and parses an OpenAI-compatible models list from |
| 315 | // the given URL using the supplied client. It is a thin wrapper around |
no test coverage detected