plugins.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import json
  2. from dataclasses import dataclass
  3. from urllib.parse import quote
  4. from importlib.resources import files
  5. from typing import Optional, List
  6. from gpustack.config.config import Config
  7. from gpustack_higress_plugins.server import router as higress_plugins_router
  8. # Reuse the same prefix as the plugin server router
  9. http_path_prefix = higress_plugins_router.prefix.removeprefix("/")
  10. @dataclass
  11. class HigressPlugin:
  12. name: str
  13. version: str
  14. def get_path(self, cfg: Optional[Config] = None) -> str:
  15. path = "/".join(
  16. [quote(self.name, safe=""), quote(self.version, safe=""), "plugin.wasm"]
  17. )
  18. return f"{get_plugin_url_prefix(cfg)}/{path}"
  19. def _load_plugins_from_manifest() -> List[HigressPlugin]:
  20. manifest_text = (
  21. files("gpustack_higress_plugins")
  22. .joinpath("manifest.json")
  23. .read_text(encoding="utf-8")
  24. )
  25. manifest = json.loads(manifest_text)
  26. return [
  27. HigressPlugin(name=name, version=info["latest"])
  28. for name, info in manifest["plugins"].items()
  29. ]
  30. supported_plugins: List[HigressPlugin] = _load_plugins_from_manifest()
  31. def get_plugin_url_with_name_and_version(
  32. name: str, version: str, cfg: Optional[Config] = None
  33. ) -> str:
  34. target = next(
  35. (p for p in supported_plugins if p.name == name and p.version == version), None
  36. )
  37. if target is None:
  38. raise ValueError(f"Plugin {name} with version {version} is not supported.")
  39. return target.get_path(cfg)
  40. def get_plugin_url_prefix(cfg: Optional[Config] = None) -> str:
  41. base_url = "http://127.0.0.1"
  42. if cfg is not None and cfg.gateway_plugin_server_url:
  43. base_url = cfg.gateway_plugin_server_url
  44. return f"{base_url}/{http_path_prefix}"