MCPcopy Create free account
hub / github.com/Chat2AnyLLM/code-agent-manager / ResolveModels

Function ResolveModels

internal/providers/models.go:70–107  ·  view source on GitHub ↗

ResolveModels returns the model list to present for endpoint ep, identified by epName. Priority: 1. Built-in /v1/models discovery → combined with ep.Models. 2. ep.Models when API discovery fails or returns no models. 3. Deprecated ep.ListModelsCmd fallback when no static models exist. 4. No source

(
	ep Endpoint,
	epName string,
	cacheTTL time.Duration,
	cacheDir string,
	getenv func(string) string,
)

Source from the content-addressed store, hash-verified

68// On list_models_cmd timeout, non-zero exit, or empty output, returns
69// ([], err). Callers treat err as a recoverable signal.
70func ResolveModels(
71 ep Endpoint,
72 epName string,
73 cacheTTL time.Duration,
74 cacheDir string,
75 getenv func(string) string,
76) ([]string, error) {
77 if cacheDir == "" {
78 cacheDir = filepath.Join(pathutil.CacheDir(), "models")
79 }
80 cachePath := filepath.Join(cacheDir, epName+".json")
81
82 if cached, ok := readModelsCache(cachePath, cacheTTL); ok {
83 return mergeModels(cached, ep.Models, defaultModelsForEndpoint(ep)), nil
84 }
85
86 models, err := fetchAPIModels(ep, getenv)
87 if err == nil && len(models) > 0 {
88 merged := mergeModels(models, ep.Models, defaultModelsForEndpoint(ep))
89 _ = writeModelsCache(cachePath, models)
90 return merged, nil
91 }
92
93 if len(ep.Models) > 0 {
94 return mergeModels(ep.Models, defaultModelsForEndpoint(ep)), nil
95 }
96 if ep.ListModelsCmd == "" {
97 return nil, nil
98 }
99
100 models, err = runListModelsCmd(ep, epName, getenv)
101 if err != nil {
102 return nil, nil
103 }
104
105 _ = writeModelsCache(cachePath, models)
106 return models, nil
107}
108
109type modelsResponse struct {
110 Data []struct {

Calls 7

CacheDirFunction · 0.92
readModelsCacheFunction · 0.85
mergeModelsFunction · 0.85
defaultModelsForEndpointFunction · 0.85
fetchAPIModelsFunction · 0.85
writeModelsCacheFunction · 0.85
runListModelsCmdFunction · 0.85