gateway_chat_listing.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """``--list-tools`` / ``--list-toolsets`` on the gateway path: a catalog listing needs no session.
  2. Main built a whole in-process CLI to print these and exit; the listing reads only the registry
  3. and this profile's CLI toolset selection, so it runs in the client without an agent or a gateway.
  4. """
  5. def _selection(args):
  6. from hermes_cli.config import load_config
  7. from hermes_cli.tools_config import _get_platform_tools
  8. requested = getattr(args, "toolsets", None)
  9. if isinstance(requested, (list, tuple)):
  10. requested = ",".join(map(str, requested))
  11. if isinstance(requested, str) and requested.strip():
  12. return sorted({name.strip() for name in requested.split(",") if name.strip()}), []
  13. config = load_config()
  14. disabled = (config.get("agent") or {}).get("disabled_toolsets") or []
  15. return sorted(_get_platform_tools(config, "cli")), list(disabled)
  16. def print_tool_listing(args) -> int:
  17. from agent.i18n import t
  18. enabled, disabled = _selection(args)
  19. if getattr(args, "list_tools", False):
  20. from model_tools import get_tool_definitions, get_toolset_for_tool
  21. # Pre-assembly list, as main's /tools: deferred tool_search tools are shown too.
  22. tools = get_tool_definitions(enabled_toolsets=enabled, disabled_toolsets=disabled, quiet_mode=True,
  23. skip_tool_search_assembly=True)
  24. if not tools:
  25. print(t("cli.tools.none_available"))
  26. return 0
  27. grouped = {}
  28. for tool in sorted(tools, key=lambda item: item["function"]["name"]):
  29. name = tool["function"]["name"]
  30. desc = tool["function"].get("description", "").split("\n")[0]
  31. if ". " in desc:
  32. desc = desc[:desc.index(". ") + 1]
  33. grouped.setdefault(get_toolset_for_tool(name) or "unknown", []).append((name, desc))
  34. print(t("cli.tools.header"))
  35. for toolset in sorted(grouped):
  36. print(f"\n [{toolset}]")
  37. for name, desc in grouped[toolset]:
  38. print(f" * {name:<20} - {desc}")
  39. print(f"\n {t('cli.tools.total', count=str(len(tools)))}")
  40. return 0
  41. from toolsets import get_all_toolsets, get_toolset_info
  42. print(t("cli.toolsets.header"))
  43. for name in sorted(get_all_toolsets()):
  44. info = get_toolset_info(name)
  45. if info:
  46. marker = "(*)" if name in enabled else " "
  47. print(f" {marker} {name:<18} [{info['tool_count']:>2} tools] - {info['description']}")
  48. print(f"\n {t('cli.toolsets.currently_enabled_legend')}")
  49. return 0