ó
    ýÞ jÒ  ã                  óž   • S r SSKJr  SSKJr  SSKJrJrJrJ	r	  SS jr
SS jr        SS jr1 SkrSS	 jrSS
 jr    SS jrSS jrg)z•Subscription matching: channel inference + namespace prefix filtering.

Direct port of `libs/sdk/src/client/stream/subscription.ts` from the JS SDK.
é    )Úannotations)ÚAny)ÚChannelÚEventÚ	NamespaceÚSubscribeParamsc                ó>   • U R                  S5      nUS:X  a  U $ U SU $ )z<Strip the dynamic suffix after `:` from a namespace segment.Ú:éÿÿÿÿN)Úfind)ÚsegmentÚidxs     Ú[/var/www/html/gaurav/venv/lib/python3.13/site-packages/langgraph_sdk/stream/subscription.pyÚnormalize_segmentr      s'   € à
�,‰,�sÓ
€CØ˜R“iˆ7Ð2 W¨T¨c ]Ð2ó    c                óœ   • [        U5      [        U 5      :”  a  g[        XSS9 H%  u  p#X2:X  a  M  SU;   a    g[        U5      U:X  a  M%    g   g)a  Whether `event_namespace` starts with `prefix`.

Segments compare literally first; if the prefix segment contains no `:`,
the candidate is also compared after its dynamic suffix is stripped.
Mirrors `is_prefix_match` in `api/langgraph_api/protocol/namespace.py`.
F)Ústrictr
   T)ÚlenÚzipr   )Úevent_namespaceÚprefixÚsegÚ	candidates       r   Úis_prefix_matchr      sW   € ô ˆ6ƒ{”S˜Ó)Ó)ØÜ˜f¸eÔD‰ˆØÓÙØ�#‹:ÙÜ˜YÓ'¨3Ó.ÙÙñ Eð r   c                óŽ   • U(       d  gU H7  n[        X5      (       d  M  Uc    g[        U 5      [        U5      -
  U::  d  M7    g   g)zCWhether `event_namespace` matches any of `prefixes` within `depth`.TF)r   r   )r   ÚprefixesÚdepthr   s       r   Únamespace_matchesr   '   sH   € ö ØÛˆÜ˜×7Ñ7ÙØ‰=ÙÜˆÓ¤# f£+Ñ-°Õ6Ùñ ð r   >   ÚtasksÚtoolsÚvaluesÚupdatesÚmessagesÚ	lifecycleÚcheckpointsc                óH  • U R                  S5      nU[        ;   a  U$ US:X  ax  U R                  S5      =(       d    0 n[        U[        5      (       a  UR                  S5      OSn[        U[        5      (       a  UR                  S5      OSnU(       a  SU 3$ S$ US:X  a  g	g)
zÃMap a protocol event's `method` to its subscription channel.

Returns `None` for unrecognized methods so new server-side channels (e.g.
from extension transformers) don't break existing clients.
ÚmethodÚcustomÚparamsÚdataNÚnameúcustom:zinput.requestedÚinput)ÚgetÚ_DIRECT_METHODSÚ
isinstanceÚdict)Úeventr'   r)   r*   r+   s        r   Úinfer_channelr3   D   s•   € ð �Y‰Y�xÓ €FØ”Ó ØˆØ�ÓØ—‘˜8Ó$×*¨ˆÜ%/°¼×%=Ñ%=ˆv�z‰z˜&Ô!À4ˆÜ#-¨d´D×#9Ñ#9ˆt�x‰x˜Ô¸tˆæ#'�˜˜ÐÐ5¨XÐ5ØÐ"Ó"ØØr   c                ód  • [        U 5      nUc  gUR                  S/ 5      nX#;  a  UR                  S5      (       a  SU;   d  gU R                  S5      =(       d    0 n[        U[        5      (       a  UR                  S/ 5      O/ n[        UUR                  S5      UR                  S5      5      $ )	z5Whether `event` should be delivered for `definition`.FÚchannelsr,   r(   r)   Ú	namespaceÚ
namespacesr   )r3   r.   Ú
startswithr0   r1   r   )r2   Ú
definitionÚchannelr5   r)   r6   s         r   Úmatches_subscriptionr;   X   sž   € ä˜EÓ"€GØ�ØØ�~‰~˜j¨"Ó-€HØÓØ×Ñ˜9×%Ñ%¨(°hÓ*>àØ�Y‰Y�xÓ ×& B€FÜ/9¸&Ä$×/GÑ/G�—
‘
˜;¨Ô+ÈR€IÜØØ�‰�|Ó$Ø�‰�wÓóð r   c                óæ  • U (       d  S/ 0$ [        5       nSn0 nSnSnU  H‹  nUR                  S/ 5       H  nUR                  U5        M     UR                  S5      nUc  SnOU(       d  U H  n	X“[        U	5      '   M     UR                  S5      n
U
c  SnMy  U(       a  M‚  X¥:”  d  M‰  U
nM�     S[	        U5      0nU(       d#  U(       a  [        UR                  5       5      US'   U(       d  X[S'   U$ )uz  Aggregate a set of subscription filters into one covering filter.

Direct port of `client/stream/index.ts:#computeUnionFilter`.

- Channels are unioned.
- Namespaces: if any subscription omits `namespaces` (wildcard), the union
  is unscoped (omits the key). Otherwise, deduplicated union.
- Depth: if any subscription omits `depth` (unbounded), the union is
  unbounded (omits the key). Otherwise, take the max. `depth=0` is a
  valid bounded value â€” never omit when all subscriptions provide it.

Args:
    subscriptions: list of `SubscribeParams`-shaped dicts.

Returns:
    A `SubscribeParams`-shaped dict covering every input.
r5   Fr   r7   Tr   )Úsetr.   ÚaddÚtupleÚsortedÚlistr!   )Úsubscriptionsr5   Úwildcard_namespacesÚnamespace_mapÚunbounded_depthÚ	max_depthÚsubÚchÚsub_namespacesÚnsÚ	sub_depthÚresults               r   Úcompute_union_filterrM   k   sõ   € ö( Ø˜BÐÐä›€HØÐØ68€MØ€OØ€IãˆØ—'‘'˜* bÖ)ˆBØ�L‰L˜Öñ *ð Ÿ™ Ó.ˆØÑ!Ø"&ÑÞ$Û$�Ø+-œe B›iÓ(ñ %ð —G‘G˜GÓ$ˆ	ØÑØ"ŠOß � YÕ%:Ø!ŠIñ ð" )¬&°Ó*:Ð;€FÞ¦=Ü# M×$8Ñ$8Ó$:Ó;ˆˆ|ÑÞØ#ˆw‰Ø€Mr   c                óâ  • [        U R                  S/ 5      5      nUR                  S/ 5       H
  nX2;  d  M
    g   U R                  S5      nUR                  S5      nU R                  S5      nUR                  S5      nUc  Uc  gUc  gXT:*  $ Uc  gU HW  nSn	U HC  n
[        XŠ5      (       d  M  Uc  Sn	  O+Uc  M!  [        U5      [        U
5      -
  U-   U::  d  MA  Sn	  O   U	(       a  MW    g   g)zÿWhether `coverer` is a superset of `target`.

Direct port of `client/stream/index.ts:filterCovers`. Depth coverage
accounts for namespace-prefix offset: a scoped coverer needs enough depth
to absorb the extra levels of any deeper target namespace prefix.
r5   Fr   r7   T)r=   r.   r   r   )ÚcovererÚtargetÚcoverer_channelsrH   Úcoverer_depthÚtarget_depthÚcoverer_namespacesÚtarget_namespacesÚtpÚcoveredÚcps              r   Úfilter_coversrY   ¡   s  € ô ˜7Ÿ;™; z°2Ó6Ó7ÐØ�j‰j˜ RÖ(ˆØÕ%Ùñ )ð —K‘K Ó(€MØ—:‘:˜gÓ&€LØ Ÿ™ \Ó2ÐØŸ
™
 <Ó0Ðð Ñ!ØÑ ØØÑØØÑ,Ð,ð Ñ Øó  ˆØˆÛ$ˆBÜ" 2×*Ñ*ÙØÑ$Ø�ÙØÑ#áÜ�2‹wœ˜R›Ñ  <Ñ/°=Õ@Ø�Ùñ %÷ ˆwÙñ  ð  r   N)r   ÚstrÚreturnrZ   )r   r   r   r   r[   Úbool)r   r   r   zlist[Namespace] | Noner   z
int | Noner[   r\   )r2   r   r[   zChannel | None)r2   r   r9   r   r[   r\   )rB   zlist[dict[str, Any]]r[   údict[str, Any])rO   r]   rP   r]   r[   r\   )Ú__doc__Ú
__future__r   Útypingr   Úlangchain_protocolr   r   r   r   r   r   r   r/   r3   r;   rM   rY   © r   r   Ú<module>rc      sw   ðñõ
 #å ç IÓ Iô3ôð(Øðà$ðð ðð 
ô	ò$€ôô(ð&3Ø'ð3àô3õl/r   