data_use.py 958 B

123456789101112131415161718192021222324252627282930
  1. """Data-use requirements of connector results, collected while one tool call dispatches.
  2. The executor binds a sink around each dispatch and connector dispatch adds to it, so a batch's entries
  3. union into the wrapper's sink and the requirement never rides the model-visible result string.
  4. """
  5. from __future__ import annotations
  6. import contextlib
  7. import contextvars
  8. from typing import Iterable, Iterator, Optional
  9. _sink: contextvars.ContextVar[Optional[set[str]]] = contextvars.ContextVar("connector_data_use", default=None)
  10. @contextlib.contextmanager
  11. def collect_data_use() -> Iterator[set[str]]:
  12. sink: set[str] = set()
  13. token = _sink.set(sink)
  14. try:
  15. yield sink
  16. finally:
  17. _sink.reset(token)
  18. def record_data_use(requires: Iterable[str]) -> None:
  19. """No-op outside an agent dispatch (execute_code RPC, MCP serve): no message is built there."""
  20. sink = _sink.get()
  21. if sink is not None:
  22. sink.update(requires)