relay_tools.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """Core NeMo Relay adapter for Hermes tool execution."""
  2. from __future__ import annotations
  3. import contextvars
  4. import json
  5. import logging
  6. from collections.abc import Callable
  7. from typing import Any
  8. from agent import relay_llm, relay_runtime
  9. logger = logging.getLogger(__name__)
  10. def execute(
  11. tool_name: str, args: dict[str, Any], callback: Callable[[dict[str, Any]], Any], *,
  12. session_id: str, tool_call_id: str | None = None, metadata: dict[str, Any] | None = None,
  13. ) -> tuple[Any, dict[str, Any]]:
  14. """Run one tool call through Relay and return its final arguments."""
  15. runtime, session, parent = relay_runtime.resolve_execution_context(session_id)
  16. if runtime is None or session is None or not runtime.managed_execution_enabled():
  17. return callback(args), args
  18. observed_args = args
  19. raw_result: dict[str, Any] = {}
  20. callback_error: BaseException | None = None
  21. callback_context = contextvars.copy_context()
  22. def guarded(final_args: dict[str, Any]) -> Any:
  23. # Everything the tool transitively calls (incl. auxiliary LLM calls on worker
  24. # threads) must bypass managed Relay: the pipeline's Futures bind to THIS loop,
  25. # which is blocked until the tool returns.
  26. # See #77244.
  27. with relay_runtime.managed_callback_guard():
  28. return callback(final_args)
  29. def invoke(next_args: Any) -> Any:
  30. nonlocal callback_error, observed_args
  31. observed_args = next_args if isinstance(next_args, dict) else args
  32. try:
  33. result = callback_context.copy().run(guarded, observed_args)
  34. except BaseException as exc:
  35. callback_error = exc
  36. raise
  37. raw_result.update(value=result, json=_jsonable(result))
  38. return runtime.relay.ToolExecutionResult(raw_result["json"])
  39. try:
  40. managed = _run_awaitable(
  41. runtime.run_in_session_async(
  42. session, runtime.relay.tools.execute, tool_name, _jsonable(args), invoke,
  43. handle=parent, metadata=_jsonable(metadata or {}), tool_call_id=tool_call_id or None,
  44. )
  45. )
  46. except BaseException as exc:
  47. if callback_error is not None and relay_runtime._is_relay_wrapped_callback_error(exc, callback_error):
  48. raise callback_error
  49. if isinstance(exc, Exception) and callback_error is None and "value" in raw_result:
  50. logger.warning(
  51. "NeMo Relay tool post-processing failed after dispatch success; returning the Hermes tool result",
  52. exc_info=True,
  53. )
  54. return raw_result["value"], observed_args
  55. raise
  56. managed_result = managed.result
  57. if "value" in raw_result and _json_equal(managed_result, raw_result["json"]):
  58. return raw_result["value"], observed_args
  59. if isinstance(managed_result, str):
  60. return managed_result, observed_args
  61. return json.dumps(_jsonable(managed_result), ensure_ascii=False), observed_args
  62. def _jsonable(value: Any) -> Any:
  63. if value is None or isinstance(value, (str, int, float, bool)):
  64. return value
  65. if isinstance(value, dict):
  66. return {str(key): _jsonable(item) for key, item in value.items()}
  67. if isinstance(value, (list, tuple, set)):
  68. return [_jsonable(item) for item in value]
  69. model_dump = getattr(value, "model_dump", None)
  70. if callable(model_dump):
  71. try:
  72. # warnings=False: pydantic's generic-union warning would leak to the CLI mid-turn.
  73. try:
  74. return _jsonable(model_dump(mode="json", warnings=False))
  75. except TypeError:
  76. return _jsonable(model_dump())
  77. except Exception:
  78. pass
  79. try:
  80. return _jsonable(vars(value))
  81. except (TypeError, AttributeError):
  82. return str(value)
  83. def _json_equal(left: Any, right: Any) -> bool:
  84. try:
  85. return relay_llm._canonical_json(left, _jsonable) == relay_llm._canonical_json(right, _jsonable)
  86. except (TypeError, ValueError):
  87. return left == right
  88. def _run_awaitable(value: Any) -> Any:
  89. return relay_llm._run_awaitable(
  90. value, loop_error="Synchronous Hermes Relay tool execution cannot run on an active event-loop thread",
  91. )