codekingpro/portable-devtools
114k
1"""Internal tracers used for `stream_log` and `astream` events implementations."""2 3import typing4from collections.abc import AsyncIterator, Iterator5from uuid import UUID6 7T = typing.TypeVar("T")8 9 10# THIS IS USED IN LANGGRAPH.11@typing.runtime_checkable12class _StreamingCallbackHandler(typing.Protocol[T]):13 """Types for streaming callback handlers.14 15 This is a common mixin that the callback handlers for both astream events and16 astream log inherit from.17 18 The `tap_output_aiter` method is invoked in some contexts to produce callbacks for19 intermediate results.20 """21 22 def tap_output_aiter(23 self, run_id: UUID, output: AsyncIterator[T]24 ) -> AsyncIterator[T]:25 """Used for internal astream_log and astream events implementations."""26 27 def tap_output_iter(self, run_id: UUID, output: Iterator[T]) -> Iterator[T]:28 """Used for internal astream_log and astream events implementations."""29 30 31# THIS IS USED IN LANGGRAPH.32class _V2StreamingCallbackHandler:33 """Marker base class for handlers that consume `on_stream_event` (v2).34 35 A handler inheriting from this class signals that it wants content-36 block lifecycle events from `stream_events(version="v3")` (and its37 async equivalent) rather than the v1 `on_llm_new_token` chunks.38 `BaseChatModel.invoke` uses39 `isinstance(handler, _V2StreamingCallbackHandler)` to decide whether40 to route an invoke through the v2 event generator.41 42 Implemented as a concrete marker class (not a `Protocol`) so opt-in43 is explicit via inheritance. An empty `runtime_checkable` Protocol44 would match every object and misroute every call. The event45 delivery contract itself lives on46 `BaseCallbackHandler.on_stream_event`.47 """48 49 50__all__ = [51 "_StreamingCallbackHandler",52 "_V2StreamingCallbackHandler",53]54 