_utils.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. from __future__ import annotations
  2. import ast
  3. import inspect
  4. import re
  5. import textwrap
  6. from collections.abc import Callable
  7. from typing import Any
  8. from langchain_core.runnables import Runnable, RunnableLambda, RunnableSequence
  9. from langgraph.checkpoint.base import ChannelVersions
  10. from typing_extensions import override
  11. from langgraph._internal._runnable import RunnableCallable, RunnableSeq
  12. from langgraph.pregel.protocol import PregelProtocol
  13. def get_new_channel_versions(
  14. previous_versions: ChannelVersions, current_versions: ChannelVersions
  15. ) -> ChannelVersions:
  16. """Get subset of current_versions that are newer than previous_versions."""
  17. if previous_versions:
  18. version_type = type(next(iter(current_versions.values()), None))
  19. null_version = version_type() # type: ignore[misc]
  20. new_versions = {
  21. k: v
  22. for k, v in current_versions.items()
  23. if v > previous_versions.get(k, null_version) # type: ignore[operator]
  24. }
  25. else:
  26. new_versions = current_versions
  27. return new_versions
  28. def find_subgraph_pregel(candidate: Runnable) -> PregelProtocol | None:
  29. from langgraph.pregel import Pregel
  30. candidates: list[Runnable] = [candidate]
  31. for c in candidates:
  32. if (
  33. isinstance(c, PregelProtocol)
  34. # subgraphs that disabled checkpointing are not considered
  35. and (not isinstance(c, Pregel) or c.checkpointer is not False)
  36. ):
  37. return c
  38. elif isinstance(c, RunnableSequence) or isinstance(c, RunnableSeq):
  39. candidates.extend(c.steps)
  40. elif isinstance(c, RunnableLambda):
  41. candidates.extend(c.deps)
  42. elif isinstance(c, RunnableCallable):
  43. if c.func is not None:
  44. candidates.extend(
  45. nl.__self__ if hasattr(nl, "__self__") else nl
  46. for nl in get_function_nonlocals(c.func)
  47. )
  48. elif c.afunc is not None:
  49. candidates.extend(
  50. nl.__self__ if hasattr(nl, "__self__") else nl
  51. for nl in get_function_nonlocals(c.afunc)
  52. )
  53. return None
  54. def get_function_nonlocals(func: Callable) -> list[Any]:
  55. """Get the nonlocal variables accessed by a function.
  56. Args:
  57. func: The function to check.
  58. Returns:
  59. List[Any]: The nonlocal variables accessed by the function.
  60. """
  61. try:
  62. code = inspect.getsource(func)
  63. tree = ast.parse(textwrap.dedent(code))
  64. visitor = FunctionNonLocals()
  65. visitor.visit(tree)
  66. values: list[Any] = []
  67. closure = (
  68. inspect.getclosurevars(func.__wrapped__)
  69. if hasattr(func, "__wrapped__") and callable(func.__wrapped__)
  70. else inspect.getclosurevars(func)
  71. )
  72. candidates = {**closure.globals, **closure.nonlocals}
  73. for k, v in candidates.items():
  74. if k in visitor.nonlocals:
  75. values.append(v)
  76. for kk in visitor.nonlocals:
  77. if "." in kk and kk.startswith(k):
  78. vv = v
  79. for part in kk.split(".")[1:]:
  80. if vv is None:
  81. break
  82. else:
  83. try:
  84. vv = getattr(vv, part)
  85. except AttributeError:
  86. break
  87. else:
  88. values.append(vv)
  89. except (SyntaxError, TypeError, OSError, SystemError):
  90. return []
  91. return values
  92. class FunctionNonLocals(ast.NodeVisitor):
  93. """Get the nonlocal variables accessed of a function."""
  94. def __init__(self) -> None:
  95. self.nonlocals: set[str] = set()
  96. @override
  97. def visit_FunctionDef(self, node: ast.FunctionDef) -> Any:
  98. """Visit a function definition.
  99. Args:
  100. node: The node to visit.
  101. Returns:
  102. Any: The result of the visit.
  103. """
  104. visitor = NonLocals()
  105. visitor.visit(node)
  106. self.nonlocals.update(visitor.loads - visitor.stores)
  107. @override
  108. def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> Any:
  109. """Visit an async function definition.
  110. Args:
  111. node: The node to visit.
  112. Returns:
  113. Any: The result of the visit.
  114. """
  115. visitor = NonLocals()
  116. visitor.visit(node)
  117. self.nonlocals.update(visitor.loads - visitor.stores)
  118. @override
  119. def visit_Lambda(self, node: ast.Lambda) -> Any:
  120. """Visit a lambda function.
  121. Args:
  122. node: The node to visit.
  123. Returns:
  124. Any: The result of the visit.
  125. """
  126. visitor = NonLocals()
  127. visitor.visit(node)
  128. self.nonlocals.update(visitor.loads - visitor.stores)
  129. class NonLocals(ast.NodeVisitor):
  130. """Get nonlocal variables accessed."""
  131. def __init__(self) -> None:
  132. self.loads: set[str] = set()
  133. self.stores: set[str] = set()
  134. @override
  135. def visit_Name(self, node: ast.Name) -> Any:
  136. """Visit a name node.
  137. Args:
  138. node: The node to visit.
  139. Returns:
  140. Any: The result of the visit.
  141. """
  142. if isinstance(node.ctx, ast.Load):
  143. self.loads.add(node.id)
  144. elif isinstance(node.ctx, ast.Store):
  145. self.stores.add(node.id)
  146. @override
  147. def visit_Attribute(self, node: ast.Attribute) -> Any:
  148. """Visit an attribute node.
  149. Args:
  150. node: The node to visit.
  151. Returns:
  152. Any: The result of the visit.
  153. """
  154. if isinstance(node.ctx, ast.Load):
  155. parent = node.value
  156. attr_expr = node.attr
  157. while isinstance(parent, ast.Attribute):
  158. attr_expr = parent.attr + "." + attr_expr
  159. parent = parent.value
  160. if isinstance(parent, ast.Name):
  161. self.loads.add(parent.id + "." + attr_expr)
  162. self.loads.discard(parent.id)
  163. elif isinstance(parent, ast.Call):
  164. if isinstance(parent.func, ast.Name):
  165. self.loads.add(parent.func.id)
  166. else:
  167. parent = parent.func
  168. attr_expr = ""
  169. while isinstance(parent, ast.Attribute):
  170. if attr_expr:
  171. attr_expr = parent.attr + "." + attr_expr
  172. else:
  173. attr_expr = parent.attr
  174. parent = parent.value
  175. if isinstance(parent, ast.Name):
  176. self.loads.add(parent.id + "." + attr_expr)
  177. def is_xxh3_128_hexdigest(value: str) -> bool:
  178. """Check if the given string matches the format of xxh3_128_hexdigest."""
  179. return bool(re.fullmatch(r"[0-9a-f]{32}", value))