MCPcopy Create free account
hub / github.com/CommonstackAI/UncommonRoute / discover

Method discover

uncommon_route/model_map.py:355–415  ·  view source on GitHub ↗

Fetch ``/v1/models`` from upstream and build the model pool. Extracts pricing and infers capabilities from upstream data. Returns the number of models discovered (0 on failure).

(self, api_key: str | None = None)

Source from the content-addressed store, hash-verified

353 # ---- discovery --------------------------------------------------------
354
355 async def discover(self, api_key: str | None = None) -> int:
356 """Fetch ``/v1/models`` from upstream and build the model pool.
357
358 Extracts pricing and infers capabilities from upstream data.
359 Returns the number of models discovered (0 on failure).
360 """
361 if not self.upstream_url:
362 return 0
363
364 models_url = f"{self.upstream_url.rstrip('/')}/models"
365 headers: dict[str, str] = {"user-agent": "uncommon-route/model-discovery"}
366 if api_key:
367 headers["authorization"] = f"Bearer {api_key}"
368
369 try:
370 async with httpx.AsyncClient(
371 timeout=httpx.Timeout(10.0, connect=5.0),
372 ) as client:
373 resp = await client.get(models_url, headers=headers)
374 if resp.status_code != 200:
375 logger.warning(
376 "Model discovery: HTTP %d from %s", resp.status_code, models_url,
377 )
378 return 0
379 data = resp.json()
380 raw = data.get("data", [])
381
382 self._pool.clear()
383 self._upstream_models.clear()
384
385 for m in raw:
386 if not isinstance(m, dict) or "id" not in m:
387 continue
388 model_id = m["id"]
389 self._upstream_models.add(model_id)
390
391 pricing = _parse_upstream_pricing(m.get("pricing"))
392 capabilities = infer_capabilities(
393 model_id,
394 pricing,
395 has_explicit_pricing=isinstance(m.get("pricing"), dict) and bool(m.get("pricing")),
396 )
397 provider_name = _provider_prefix(model_id) or m.get("owned_by", "unknown")
398
399 self._pool[model_id] = DiscoveredModel(
400 id=model_id,
401 provider=provider_name,
402 owned_by=m.get("owned_by", ""),
403 pricing=pricing,
404 capabilities=capabilities,
405 )
406
407 self._build_map()
408 self._discovered = True
409 self._last_discovery_time = time.monotonic()
410 return len(self._upstream_models)
411 except httpx.ConnectError:
412 logger.warning("Model discovery: cannot connect to %s", models_url)

Callers 4

_cmd_doctorFunction · 0.95
_on_startupFunction · 0.80
_rediscovery_loopFunction · 0.80

Calls 8

_build_mapMethod · 0.95
_parse_upstream_pricingFunction · 0.85
infer_capabilitiesFunction · 0.85
_provider_prefixFunction · 0.85
DiscoveredModelClass · 0.85
getMethod · 0.45
clearMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected