Ë
    òÏjÒ  ã                  ó�   — d Z ddlmZ ddlmZ ddlmZmZmZm	Z	 dd„Z
dd„Z	 	 	 	 	 	 	 	 dd„Zh d£Zdd	„Zdd
„Z	 	 	 	 dd„Zdd„Zy)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                ó<   — | j                  d«      }|dk(  r| S | d| S )z<Strip the dynamic suffix after `:` from a namespace segment.Ú:éÿÿÿÿN)Úfind)ÚsegmentÚidxs     ú^/var/www/html/gaurav-v2/venv/lib/python3.12/site-packages/langgraph_sdk/stream/subscription.pyÚnormalize_segmentr      s'   € à
�,‰,�sÓ
€CØ˜R’iˆ7Ð2 W¨T¨c ]Ð2ó    c                ó˜   — t        |«      t        | «      kD  ryt        || d¬«      D ]!  \  }}||k(  rŒd|v r yt        |«      |k(  rŒ! y y)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      s]   € ô ˆ6ƒ{”S˜Ó)Ò)ØÜ˜f o¸e×D‰ˆˆYØ˜ÒØØ�#‰:ÙÜ˜YÓ'¨3Ò.ØÙð Eð r   c                ór   — |sy|D ]/  }t        | |«      sŒ|€ yt        | «      t        |«      z
  |k  sŒ/ y y)zCWhether `event_namespace` matches any of `prefixes` within `depth`.TF)r   r   )r   ÚprefixesÚdepthr   s       r   Únamespace_matchesr   '   sG   € ñ ØÛˆÜ˜°Ô7ØØˆ=ÙÜˆÓ¤# f£+Ñ-°Ó6Ùð ð r   >   ÚtasksÚtoolsÚvaluesÚupdatesÚmessagesÚ	lifecycleÚcheckpointsc                ó  — | j                  d«      }|t        v r|S |dk(  rd| j                  d«      xs i }t        |t        «      r|j                  d«      nd}t        |t        «      r|j                  d«      nd}|rd|› �S dS |dk(  ry	y)
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ˆt�x‰x˜Ô¸tˆá#'�˜˜ÐÐ5¨XÐ5ØÐ"Ò"ØØr   c                ó>  — t        | «      }|€y|j                  dg «      }||vr|j                  d«      rd|v sy| j                  d«      xs i }t        |t        «      r|j                  dg «      ng }t        ||j                  d«      |j                  d«      «      S )	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Ø�hÑØ×Ñ˜9Ô%¨(°hÑ*>àØ�Y‰Y�xÓ Ò& B€FÜ/9¸&Ä$Ô/G�—
‘
˜;¨Ô+ÈR€IÜØØ�‰�|Ó$Ø�‰�wÓóð r   c                ó˜  — | sdg iS t        «       }d}i }d}d}| D ]x  }|j                  dg «      D ]  }|j                  |«       Œ |j                  d«      }|€d}n|s|D ]  }	|	|t        |	«      <   Œ |j                  d«      }
|
€d}Œn|rŒq|
|kD  sŒw|
}Œz dt	        |«      i}|s|rt        |j                  «       «      |d<   |s||d<   |S )uª  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                ó²  — t        | j                  dg «      «      }|j                  dg «      D ]  }||vsŒ y | j                  d«      }|j                  d«      }| j                  d«      }|j                  d«      }|€|€y|€y||k  S |€y|D ]F  }d}	|D ]9  }
t        ||
«      sŒ|€d}	 n&|€Œt        |«      t        |
«      z
  |z   |k  sŒ7d}	 n |	rŒF y y)a  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 rÔ*ØØÐ$Ø�ÙØÐ#àÜ�2‹wœ˜R›Ñ  <Ñ/°=Ó@Ø�Ùð %ò Ùð  ð  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   