
    k
i\                       d dl mZ d dlZd dlZd dlmZ d dlmZmZmZ d dl	m
Z
 d dlmZmZmZmZmZmZ d dlmZmZmZmZmZmZmZmZmZmZmZ d dlmZ d d	l m!Z" d d
l#m$Z$ d dl%m&Z& d dl'm(Z(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< ddl=m>Z>  e-            Z? e>            Z@d dlAmBZC  G d de4e"e&f         e          ZDdS )    )annotationsN)asynccontextmanager)datetime	timedeltatimezone)TracebackType)AnyAsyncIteratorIterableOptionalSequencecast)GetOpIndexConfigListNamespacesOpOpPutOpResultSearchOp	TTLConfigensure_embeddingsget_text_at_pathtokenize_path)AsyncBatchedBaseStore)Redis)Query)AsyncSearchIndex)FilterQueryVectorQuery)RedisConnectionFactory)TokenEscaper)ULID)REDIS_KEY_SEPARATORSTORE_PREFIXSTORE_VECTOR_PREFIXBaseRedisStoreRedisDocument
_decode_ns_ensure_string_or_literal
_group_ops_namespace_to_text_row_to_item_row_to_search_itemlogger   )TokenUnescaper)RedisClusterc            	      n   e Zd ZU dZded<   ded<   ded<   dZded<   d	Zd
ed<   d	Zded<   d	Zded<   d	Z	ded<   	 dUd	d	d	d	d	e
eddVd!Z	 	 	 dWdXd"ZdYd#ZdYd$Z	 	 dZd[d+Zd\d-Z	 dUd]d0ZdUd^d2Zeed	d	e
ed3d_d6                        ZdYd7Zd`d8Z	 	 	 dWdad?ZdbdCZdcdEZdddIZdedMZdfdNZdgdQZdhdTZ d	S )iAsyncRedisStorezAsync Redis store with optional vector search.

    Supports standard Redis URLs (redis://), SSL (rediss://), and
    Sentinel URLs (redis+sentinel://host:26379/service_name/db).
    r   store_indexvector_indexbool_owns_its_clientTsupports_ttlNzasyncio.Event | None_async_ttl_stop_eventzasyncio.Task | None_ttl_sweeper_taskOptional[TTLConfig]
ttl_configOptional[bool]cluster_mode)redis_clientindexconnection_argsttlr>   store_prefixvector_prefix	redis_urlOptional[str]r?   Optional[AsyncRedis]r@   Optional[IndexConfig]rA   Optional[dict[str, Any]]rB   rC   strrD   returnNonec                  ||t          d          t          j        |            |                     |||pi            |$t	          |t
                    st          d          t          j        | | j        |||||           ddl	m
}	 | j        | j        t          z   dd	d
dddddddddddddddddgd}
t          j        |
| j                  | _        | j        rX| j        | j        t          z   dd	d
dddddddddddddddddddddddgd}|                    dg           }d}|D ]4}t	          |t&                    r|                    d          dk    r|} n5|rtdd| j        d         ddd d!t)          | j                            d"d#                             d$|d%<   d&| j        v r&|d%                             | j        d&                    	 t          j        || j                  | _        dS # t.          $ r(}t          d'| d(t1          |                     |d}~ww xY wdS ))zAInitialize store with Redis connection and optional index config.Nz1Either redis_url or redis_client must be provided)rE   r?   rA   z&cluster_mode must be a boolean or None)connr@   rB   r>   rC   rD   r   )deepcopyjson)nameprefixstorage_typerR   text)rQ   typekeytag
created_atnumeric
updated_atttl_minutes
expires_at)r@   fieldsr?   
field_name	embeddingvectorr]   rQ   flatfloat32dimsCOSINEIPL2)cosineinner_productl2distance_typerh   )	algorithmdatatyperd   distance_metricattrsann_index_configz+Failed to create vector index with schema: z	. Error: )
ValueErrorr   __init__configure_client
isinstancer6   	TypeErrorr&   _rediscopyrO   rC   r#   r   	from_dictr4   index_configrD   getdictr)   updater5   	ExceptionrJ   )selfrE   r?   r@   rA   rB   r>   rC   rD   rO   store_schemavector_schemavector_fieldsvector_fieldfes                   C:\Users\Dell Inspiron 16\Desktop\tws\AgrotaPowerBi\back-agrota-powerbi\mcp-client-agrota\venv\Lib\site-packages\langgraph/store/redis/aio.pyrr   zAsyncRedisStore.__init__J   s    !5PQQQ 	&t,,, 	%+1r 	 	
 	
 	
 #J|T,J,J#DEEE 	%%'	
 	
 	
 	
 	"!!!!! )+.AA &  "622..%y99%y99&	::%y99
 
 ,5t{
 
 

  6	 !."03FF$*  &v66"E22)599((;;)9==)9==*I>>)9==	 M" *--h;;ML"  a&& 155==K+G+G#$LE X "( ) -f5"*)-"( (
 2 -11/8LL (	) )W%  &)::: )001BCU1VWWW$4$>!% % %!!!     b-bbZ]^_Z`Z`bb g6	 6	s   * H 
H>#H99H>c                `    |du | _         |t          j        |fi |pi | _        dS || _        dS )zConfigure the Redis client.

        Supports standard Redis URLs (redis://), SSL (rediss://), and
        Sentinel URLs (redis+sentinel://host:26379/service_name/db).
        N)r7   r    get_async_redis_connectionrv   )r~   rE   r?   rA   s       r   rs   z AsyncRedisStore.configure_client   sQ     !- 40K -3 DKKK 'DKKK    c                  K   | j         r,t          | j                             d                    | _        | j        |                                  d{V  nt          j        d| j         d           | j        	                    d           d{V  | j         r#| j
        	                    d           d{V  dS dS )zInitialize store indices.embedNz%Redis cluster_mode explicitly set to z, skipping detection.F)	overwrite)ry   r   rz   
embeddingsr>   _detect_cluster_moder.   infor4   creater5   r~   s    r   setupzAsyncRedisStore.setup   s       	/!%%g.. DO
 $++----------K`8I```  
 %%%666666666 	<#**U*;;;;;;;;;;;	< 	<r   c                   K   t          | j        t                    rd| _        t	          j        d           dS d| _        t	          j        d           dS )zGDetect if the Redis client is a cluster client by inspecting its class.Tz2Redis cluster client detected for AsyncRedisStore.Fz5Redis standalone client detected for AsyncRedisStore.N)rt   rv   AsyncRedisClusterr>   r.   r   r   s    r   r   z$AsyncRedisStore._detect_cluster_mode   s\       dk#455 	Q $DKLMMMMM %DKOPPPPPr   main_keyrelated_keysOptional[list[str]]r[   Optional[float]r	   c                  K   |*| j         r#d| j         v r| j                             d          }|t          |dz            }| j        rM| j                            ||           d{V  |r&|D ]%}| j                            ||           d{V  $dS dS | j                            d          }|                    ||           |r|D ]}|                    ||           |                                 d{V  dS dS )zApply Redis native TTL to keys asynchronously.

        Args:
            main_key: The primary Redis key
            related_keys: Additional Redis keys that should expire at the same time
            ttl_minutes: Time-to-live in minutes
        Ndefault_ttl<   Ttransaction)r<   rz   intr>   rv   expirepipelineexecute)r~   r   r   r[   ttl_secondsrV   r   s          r   _apply_ttl_to_keysz"AsyncRedisStore._apply_ttl_to_keys   sx       A=DO#C#C"o11-@@"kB.//K  )k((;????????? C+ C C"k00kBBBBBBBBBBC CC C  ;//D/AA +666   :+ : : [9999&&(((((((((((% #"r   r   c                
   K   dS )a  Clean up any remaining expired items.

        This is not needed with Redis native TTL, but kept for API compatibility.
        Redis automatically removes expired keys.

        Returns:
            int: Always returns 0 as Redis handles expiration automatically
        r    r   s    r   	sweep_ttlzAsyncRedisStore.sweep_ttl  s       qr   _sweep_interval_minutesOptional[int]c                
   K   dS )zStart TTL sweeper.

        This is a no-op with Redis native TTL, but kept for API compatibility.
        Redis automatically removes expired keys.

        Args:
            _sweep_interval_minutes: Ignored parameter, kept for API compatibility
        Nr   )r~   r   s     r   start_ttl_sweeperz!AsyncRedisStore.start_ttl_sweeper)  s       	r   _timeoutc                
   K   dS )a  Stop TTL sweeper.

        This is a no-op with Redis native TTL, but kept for API compatibility.

        Args:
            _timeout: Ignored parameter, kept for API compatibility

        Returns:
            bool: Always True as there's no sweeper to stop
        Tr   )r~   r   s     r   stop_ttl_sweeperz AsyncRedisStore.stop_ttl_sweeper8  s       tr   )r@   rB   rC   rD   conn_stringAsyncIterator[AsyncRedisStore]c                 K    | |||||          4 d{V 	 }|                                  d{V  |                                 d{V  |W V  ddd          d{V  dS # 1 d{V swxY w Y   dS )z*Create store from Redis connection string.)rE   r@   rB   rC   rD   N)r   aset_client_info)clsr   r@   rB   rC   rD   stores          r   from_conn_stringz AsyncRedisStore.from_conn_stringF  s@      3!%'
 
 
 
	 
	 
	 
	 
	 
	 
	 
	 ++--((*********KKKK
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	s   :A''
A14A1c                    t          j        | j        d         | j                  | _        | j        r-t          j        | j        d         | j                  | _        dS dS )zCreate async indices.r   r^   r/   N)r   rx   SCHEMASrv   r4   ry   r5   r   s    r   create_indexeszAsyncRedisStore.create_indexes^  sm    +5LO$+
 
 
  	 0 :Qdk! ! !D	 	r   c                >   K   |                                   d{V  | S )zAsync context manager enter.N)r   r   s    r   
__aenter__zAsyncRedisStore.__aenter__h  s1       ##%%%%%%%%%r   	_exc_typeOptional[type[BaseException]]
_exc_valueOptional[BaseException]
_tracebackOptional[TracebackType]c                |  K   t          | d          r]| j        V| j                                        s=| j                                         	 | j         d{V  n# t          j        $ r Y nw xY w| j        rE| j                                         d{V  | j        j	        
                                 d{V  dS dS )zAsync context manager exit._taskN)hasattrr   donecancelasyncioCancelledErrorr7   rv   acloseconnection_pool
disconnect)r~   r   r   r   s       r   	__aexit__zAsyncRedisStore.__aexit__o  s       4!! 	dj&<TZ__EVEV&<Jj        )      	;+$$&&&&&&&&&+-88:::::::::::	; 	;s   A A-,A-opsIterable[Op]list[Result]c                4  K   t          |          \  }}dg|z  }g }t          |v rq|                    |                     t	          t          t          t          t          t          f                  |t                                       |                     t          |v rp|                    | 	                    t	          t          t          t          t          t          f                  |t                                                            t          |v rq|                    |                     t	          t          t          t          t          t          f                  |t                                       |                     t          |v rq|                    |                     t	          t          t          t          t          t          f                  |t                                       |                     t          j        |  d{V  |S )z+Execute batch of operations asynchronously.N)r*   r   append_batch_get_opslistr   tupler   r   _batch_put_opsr   _batch_search_opsr   _batch_list_namespaces_opsr   gather)r~   r   grouped_opsnum_opsresultstaskss         r   abatchzAsyncRedisStore.abatch  s     )#W!% 0KLL##d5e#45{57IJJKKW    KLL##d5e#45{57IJJKK    {""LL&&d5h#78+h:OPPQQ    {**LL// s,<'<!=>'(89    
 
 
 ne$$$$$$$$r   r~   c                
   	 t          j                    rt          j        d          n# t          $ r Y nw xY wt          j        |                     |          t          j                                                              S )a  Execute batch of operations synchronously.

        Args:
            ops: Operations to execute in batch

        Returns:
            Results from batch execution

        Raises:
            asyncio.InvalidStateError: If called from the main event loop
        zSynchronous calls to AsyncRedisStore are only allowed from a different thread. From the main thread, use the async interface.For example, use `await store.abatch(...)` or `await store.aget(...)`)r   get_running_loopInvalidStateErrorRuntimeErrorrun_coroutine_threadsafer   get_event_loopresult)r~   r   s     r   batchzAsyncRedisStore.batch  s    		')) /'    	 	 	D	/KKg466
 

&((	s   '* 
77get_opsSequence[tuple[int, GetOp]]r   c                   K   i }|                      |          D ].\  }}}}| j                            t          |                     d{V }d |j        D             }	|D ]\  }
}||	v r|	|         \  }}t          ||| j                  ||
<   d}t          |          D ]\  }\  }}||
k    r|} n|||         d         }t          |d          rs|j	        rl|
|vrg ||
<   ||
         
                    |           |                    d          d         }| j         t           | }||
         
                    |           0|r=| j        r7d}d| j        v r| j                            d          }|t!          |d	z            }| j        re|                                D ]N}|D ]I}| j                            |           d{V }|d
k    r!| j                            ||           d{V  JOdS | j                            d          }|                                D ]C}|D ]>}| j                            |           d{V }|d
k    r|                    ||           ?D|                                 d{V  dS dS dS dS )z/Execute GET operations in batch asynchronously.Nc                    i | ]@}t          j        |j                   d          t          j        |j                   |j        fAS )rV   )rP   loadsid).0docs     r   
<dictcomp>z2AsyncRedisStore._batch_get_ops.<locals>.<dictcomp>  sN        
38$$U+dj.B.BCF-K  r   deserialize_fnr/   refresh_ttl:r   r   r   Tr   )_get_batch_GET_ops_queriesr4   searchr   docsr,   _deserialize_value	enumerater   r   r   splitrD   r#   r<   rz   r   r>   valuesrv   rB   r   r   r   )r~   r   r   refresh_keys_by_idxquery_	namespaceitemsres
key_to_rowidxrV   datadoc_idop_idxi	local_idxopdoc_uuid
vector_keyr[   r   keys_to_refreshrB   r   keyss                             r   r   zAsyncRedisStore._batch_get_ops  s       	 +/*I*I'*R*R #	H #	H&E1i(//e========C 8  J
 " H HS*$$#-c?LD&#/!48O$ $ $GCL "F.7.@.@ " "*?Ir$++%&F!E , )$V_Q/"2}55 
H". 
H"*===;= 3C 8/4;;FCCC (.||C'8'8'<H#'#5 V7J VH V V ' 04;;JGGG7H<  	-4? 	-K//"o11-@@&!+"233$ -+>+E+E+G+G K K#2 K KC(,(<(<"<"<"<"<"<"<C"Qww&*k&8&8k&J&J J J J J J J JKK K  ${33$(  4    H !4 : : < < B B#' B BC(,(<(<"<"<"<"<"<"<C"Qww ([ A A A	B #**,,,,,,,,,,,7	- 	- 	- 	- '&r   put_opsSequence[tuple[int, PutOp]]Qtuple[list[RedisDocument], Optional[tuple[str, list[tuple[str, str, str, str]]]]]c           
       K   i }|D ]\  }}|||j         |j        f<   g }g }|                                D ]4}|j        |                    |           |                    |           5g }d}g }	|rv|D ]s}t          |j                   }
d|
 d|j         d}| j                            |           d{V }|j        D ]'}| j	        
                    |j                   d{V  (t|r|D ]}t          t          j        t          j                                                  dz            }d}d}|j        Z|j        }t          t          j        t          j                  t'          |j                  z                                             }t)          t          |j                   |j        |                     |j                  ||||          }|                    |           | j        r|j        durw|j        | j        d	         nd
 |j        D             }|D ]O\  }}t1          |j        |          }|D ]2}|	                    t          |j                   |j        ||f           3P|	rd|	f}||fS )z7Prepare queries - no Redis operations in async version.Nz	(@prefix: @key:{z})i@B )minutes)rR   rV   valuerX   rZ   r[   r\   F__tokenized_fieldsc                0    g | ]}|t          |          fS r   )r   )r   ixs     r   
<listcomp>z?AsyncRedisStore._aprepare_batch_PUT_queries.<locals>.<listcomp>X  s%    III"r=#4#45IIIr    )r   rV   r   r  r   r+   r4   r   r   rv   deleter   r   r   nowr   utc	timestamprB   r   r'   _serialize_valuery   r@   r   )r~   r
  dedupped_opsr   r  insertsdeletes
operationsembedding_requestto_embedrR   r   r   r   r  r[   r\   pathspathtokenized_pathtextsrT   s                         r   _aprepare_batch_PUT_queriesz+AsyncRedisStore._aprepare_batch_PUT_queries  s      BD 	6 	6EAr35L",/00!!%%'' 	# 	#Bxr""""r""""*,
 46 	5 5 5+BL99?F??BF??? $ 0 7 7 > >>>>>>>"< 5 5C+,,SV44444444445  )	3 % %(,x|44>>@@9LMM #!
6%"$&K!$$L66269R9R9RR#)++" "J $-bl;;//99"" +)   !!#&&&$ )>)> 8+ )*>??IIIII  16  ,n 0> J J$)  D$OO!3BL!A!A264QU V   
  3%'N!,,,r   c                
  K   |                      |           d{V \  }}|D ]\  }}t          |j                  }d| dt                              |j                   d}| j                            |           d{V }| j        r|j	        D ]'}	| j
                            |	j                   d{V  (| j        rO| j                            |           d{V }
|
j	        D ]'}| j
                            |j                   d{V  (| j
                            d          }|j	        D ]}	|                    |	j                   | j        rD| j                            |           d{V }
|
j	        D ]}|                    |j                   |j        r|                                 d{V  i }g }g }i }|D ]|\  }}|j        pt'          t)                                }t          |j                  }||||j        f<   t+          |d          r%|j        | j         t0           | }g |j        f||<   }|D ]}	|	d         |	d	         f}||         }d
|	v r|	                    d
d           d|	v r|	                    dd           |                    |	           | j         t0           | }|                    |           |ri| j        r@t7          |          D ]/\  }}| j                            |g||         g           d{V  0n"| j                            ||           d{V  |r| j        r|\  }}| j                            d |D                        d{V }g }g }t?          ||          D ]\  \  }}}}}||f} ||          }|                    |||t+          |d          r|                                 n|tC          j"        tF          j$                  %                                tC          j"        tF          j$                  %                                d           | j&         t0           | }!|                    |!           | j         t0           | }||v r!||         d                             |!           |ri| j        r@t7          |          D ]/\  }}"| j                            |"g||         g           d{V  0n"| j                            ||           d{V  |'                                D ]%\  }\  }#}$| (                    ||#|$           d{V  &dS )z/Execute PUT operations in batch asynchronously.N@prefix:r  }Tr   rB   rR   rV   r[   r\   )r	  c                    g | ]	\  }}}}|
S r   r   )r   r   rT   s      r   r  z2AsyncRedisStore._batch_put_ops.<locals>.<listcomp>  s     777-!Q4777r   tolist)rR   rV   r_   r`   rX   rZ   r   ))r%  r+   r   _token_escaperescaperV   r4   r   r>   r   rv   r  r   ry   r5   r   command_stackr   r  rJ   r"   r   rB   rC   r#   popr   r   loadr   aembed_documentszipr*  r   r  r   r  r  rD   r   r   )%r~   r
  r  r  r   r  r   r   r   r   vector_resultsdoc_vecr   doc_ids
store_docs
store_keysttl_trackinggenerated_doc_idr   	store_keyr  	redis_keyr  store_doc_itemtext_paramsvectorsvector_docsvector_keysnsrV   r"  ra   r  redis_vector_keyvector_doc_itemr   r[   s%                                        r   r   zAsyncRedisStore._batch_put_opsg  s     
 /3.N.Nw.W.W(W(W(W(W(W(W%
%  	- 	-EAr*2<88ISySS.2G2G2O2OSSSE ,33E::::::::G  -"< 5 5C+,,SV4444444444$ =+/+<+C+CE+J+J%J%J%J%J%J%JN#1#6 = ="k00<<<<<<<<<<;//D/AA"< , ,COOCF++++$ 4+/+<+C+CE+J+J%J%J%J%J%J%JN#1#6 4 4 
3333 *- #**,,,,,,,,, /1*,
 "
 	
  
	: 
	:EArx##&tvv;; .r|<<	/?BF+,2u%% :"&*<,U.AUCSUU  /1"&\L*  	) 	)CXE
3IY'F##t,,,s""d+++c""",K.AK6KKIi(((( 	I  I *3:)>)> X X%A~*//0@
ST/WWWWWWWWWWX &++JZ+HHHHHHHHH  '	P '	P.NA{ O<<77;777       G 13K%'K.1+w.G.G G G*"S$F/13i
 ,"""$"&*/6vx/H/HTFMMOOOf&.l8<&@&@&J&J&L&L&.l8<&@&@&J&J&L&L	 	   '+&8#W:M#Wv#W#W ""#3444 #/N1DNfNN|++ *1-445EFFF P$ P.7.D.D  *?"/44,-[^4D 5          
 +00;0OOOOOOOOO 6B5G5G5I5I 	O 	O1H1|[))(L+NNNNNNNNNN	O 	Or   
search_opsSequence[tuple[int, SearchOp]]c           
       K   |                      |          \  }}i }|rX| j        rQ| j                            d |D                        d{V }t          t	          d |D             |                    }t	          ||          D ]\\  \  }}\  }	}
}}|j        r||v r||         }t          t          |d          r|                                n|ddt          |j
                   dg d|	          }|                    ||           | j                            |           d{V }i }| j        rg }|D ]}t          |t                    r|                    d
          nt!          |d
d          }|ry|                    d          d         }| j         t&           | }|||<   | j                                                            |           d{V }|                    |           |}n| j                            d          }|D ]}t          |t                    r|                    d
          nt!          |d
d          }|rY|                    d          d         }| j         t&           | }|||<   |                                                    |           |                                 d{V }g }g }t3          |          }|                                D ]}t7          |d          }|r||         }t          |t                    r|                    d          nt!          |dd          }|dt9          |          z
  nd}t          |t                    sI	 t+          j        |          }n3# t*          j        t>          f$ r tA          j!        d|            Y w xY wt          |t                    r||d<   ntA          j!        d|            |j"        rxd} |                    di           }!|j"        #                                D ]B\  }"}#|!                    |"          }$t          |#tH                    r	|$|#vrd}  n8|$|#k    rd}  nC| s|j%        rW|                    |           |                    d          d         }| j&         t&           | }%|                    |%           |                    tO          tQ          |d                   ||| j)                             |j%        r|r| j*        r
d}&d| j*        v r| j*                            d          }&|&tW          |&dz            }'| j        rM|D ]I}"| j        ,                    |"           d{V }(|(dk    r!| j        -                    |"|'           d{V  Jn}| j                            d          }|D ]>}"| j        ,                    |"           d{V }(|(dk    r|-                    |"|'           ?|j.        r|                                 d{V  |||<   t_          |	                              ||          })| j0        1                    |)           d{V }*g }g }|*j2        D ]?}t+          j        |j                  }+|j"        rwd} |+                    di           }!|j"        #                                D ]B\  }"}#|!                    |"          }$t          |#tH                    r	|$|#vrd}  n8|$|#k    rd}  nC| s|j%        ra|                    |j3                   |j3                            d          d         }| j&         t&           | }%|                    |%           |                    tO          tQ          |+d                   |+| j)                             A|j%        r|r| j*        r
d}&d| j*        v r| j*                            d          }&|&tW          |&dz            }'| j        rM|D ]I}"| j        ,                    |"           d{V }(|(dk    r!| j        -                    |"|'           d{V  Jn}| j                            d          }|D ]>}"| j        ,                    |"           d{V }(|(dk    r|-                    |"|'           ?|j.        r|                                 d{V  |||<   ^dS )z2Execute search operations in batch asynchronously.c                    g | ]\  }}|S r   r   )r   r   r   s      r   r  z5AsyncRedisStore._batch_search_ops.<locals>.<listcomp>  s    :::81e:::r   Nc                    g | ]\  }}|S r   r   )r   r   r   s      r   r  z5AsyncRedisStore._batch_search_ops.<locals>.<listcomp>  s    %K%K%Kfc1c%K%K%Kr   r*  r`   r'  *)rR   rV   vector_distance)ra   vector_field_namefilter_expressionreturn_fieldsnum_resultsr   r   r/   Fr   rI  r   g      ?g        zFailed to parse store_doc: z/store_doc is not a dict after parsing attempt: Tr  r   rR   )scorer   r   r   r   )4_get_batch_search_queriesr   r0  r{   r1  r   r   r   r*  r+   namespace_prefixpagingr5   r>   rt   rz   getattrr   rC   r#   rv   rP   r   r   r   iterr	  nextfloatr   JSONDecodeErrorru   r.   errorfilterr   r   r   rD   r-   r(   r   r<   r   rB   r   r-  r   r4   r   r   r   ),r~   rC  r   queriesembedding_requestsquery_vectorsr=  r   r  	query_strparamslimitoffsetra   vector_queryvector_results_docs
result_mapr5  r   r  r  r9  r;  store_docs_rawr   r   refresh_keysstore_docs_iter	store_docvector_resultdistrN  matchesr  rV   expectedactualr  r[   r   rB   r   r   r   s,                                               r   r   z!AsyncRedisStore._batch_search_ops  s
      '+&D&DZ&P&P##  	W$/ 	W O<<::'9:::       G !%K%K8J%K%K%KW!U!UVVM >AW=U=U a	% a	%9IS"9	65&x `%C=00&s+*.5fh.G.GS6==???V&1&[1CBDW1X1X&[&[&["F"F"F %      ##FE222,0,=,C,CL,Q,Q&Q&Q&Q&Q&Q&Q#  
$ %>!#J2 > >  *#t44:CGGDMMM!(dD!9!9 
 " >'-||C'8'8';H#'#4 U6I U8 U U & 58Jy137;3C3C3E3E3I3I)3T3T-T-T-T-T-T-TN&--n===%/NN#{333FFH ,; ;  *#t44:CGGDMMM!(dD!9!9 
 " ;'-||C'8'8';H#'#4 U6I U8 U U & 58Jy1$MMOO//	:::+3+;+;+=+=%=%=%=%=%=%=N !"&~"6"6!+!2!2 B BI $_d ; ;I  @(29(=  *->>NM--.?@@@!(8I1!M!M  8<7GuT{{!2!2S))T:: )),0J$--" -"		 %)$8)#D ) ) ) &-V9-V-V W W W () &%t  	% <@I&788 #L ]R[ ] ]   % 9 )&*G$-MM'2$>$>E131B1B * *X).3#-h#=#= !*'-X'='=27(- (> &,x%7%7.3G$)E &8 $+ ) ( > <(//	:::%.__S%9%9"%=F#'#5 T7J TF T T ' )//
;;;/ *9X+> ? ? )&+/3/F	     > 9l 9t 9"&K$77&*o&9&9-&H&H".&)+*:&;&;, 9'3 O O,0KOOC,@,@&@&@&@&@&@&@#&77*.+*<*<S+*N*N$N$N$N$N$N$N$NO
 (,{';';';'M'MH'3 F F,0KOOC,@,@&@&@&@&@&@&@#&77$,OOC$E$E$E'5 9&.&6&6&8&8 8 8 8 8 8 8 8$
 i((//>> !,33E::::::::!8 " "C:ch//Dy %"& $" 5 5-/Y__->-> & &MC%*YYs^^F)(D99 &#)#9#9.3G$)E $: "(8!3!3*/ % "4  ' %$ ~ 8$++CF333!$c!2!22!6#1P3FPPP # %++J777LL+&tH~66 +/+B      > 9l 9t 9"&K$77&*o&9&9-&H&H".&)+*:&;&;, 9'3 O O,0KOOC,@,@&@&@&@&@&@&@#&77*.+*<*<S+*N*N$N$N$N$N$N$N$NO
 (,{';';';'M'MH'3 F F,0KOOC,@,@&@&@&@&@&@&@#&77$,OOC$E$E$E'5 9&.&6&6&8&8 8 8 8 8 8 8 8$Ca	% a	%s   5N

-N:9N:list_ops&Sequence[tuple[int, ListNamespacesOp]]c                h  K   |D ]\  }}d}|j         rg }|j         D ]v}|j        dk    r0t          |j        d          }|                    d| d           =|j        dk    r.t          |j        d          }	|                    d|	            w|rd                    |          }t          |dg	          }
| j                            |
           d
{V }t                      }|j
        D ]|}t          |d          rjt          t                              |j                                      d                    }|j        |d
|j                 }|                    |           }t'          |          }|j        s|j        r|j        pd}|j        pd}||||z            }|||<   d
S )z,Execute list namespaces operations in batch.rH  rR   T)handle_wildcardsr'  suffixz	@prefix:* )rK  rL  N.r   
   )match_conditions
match_typer+   r"  r   joinr   r4   r   setr   r   r   _token_unescaperunescaperR   r   	max_depthaddsortedr^  r_  )r~   rl  r   r   r  
base_query
conditions	conditionrR   rp  r   r   
namespacesr   r@  sorted_namespacesr_  r^  s                     r   r   z*AsyncRedisStore._batch_list_namespaces_ops  s        (	- (	-GCJ" 6
!#!4 
@ 
@I +x77!3%NT" " " #))*>V*>*>*>????"-99!3%NT" " " #))*>f*>*>??? 6!$*!5!5J  *XJWWWE(//66666666C Jx ' '3)) '/88DDJJ3OOPPB|/",/NN2&&& !'z 2 2x O29 OaB$5fv~6M$N!,GCLLQ(	- (	-r   )N)rE   rF   r?   rG   r@   rH   rA   rI   rB   r;   r>   r=   rC   rJ   rD   rJ   rK   rL   )NNN)rE   rF   r?   rG   rA   rI   rK   rL   )rK   rL   )NN)r   rJ   r   r   r[   r   rK   r	   )rK   r   )r   r   rK   rL   )r   r   rK   r6   )r   rJ   r@   rH   rB   r;   rC   rJ   rD   rJ   rK   r   )rK   r3   )r   r   r   r   r   r   rK   rL   )r   r   rK   r   )r~   r3   r   r   rK   r   )r   r   r   r   rK   rL   )r
  r  rK   r  )r
  r  rK   rL   )rC  rD  r   r   rK   rL   )rl  rm  r   r   rK   rL   )!__name__
__module____qualname____doc____annotations__r8   r9   r:   r<   r>   r$   r%   rr   rs   r   r   r   r   r   r   classmethodr   r   r   r   r   r   r   r   r%  r   r   r   r   r   r   r3   r3   6   s          "!!!""""L266666-11111&*J****#'L'''' $(v .2'+48#''+(0v v v v v vt $(-148	' ' ' ' '(< < < <*Q Q Q Q -1'+	$) $) $) $) $)N	 	 	 	 8<         
 (,#'(0      [,       48.2.2	; ; ; ; ;(, , , ,\   4K- K- K- K-ZM- M- M- M-^{O {O {O {Ozr% r% r% r%h.- .- .- .- .- .-r   r3   )E
__future__r   r   rP   
contextlibr   r   r   r   typesr   typingr	   r
   r   r   r   r   langgraph.store.baser   r   r   r   r   r   r   r   r   r   r   langgraph.store.base.batchr   redis.asyncior   
AsyncRedisredis.commands.search.queryr   redisvl.indexr   redisvl.queryr   r   redisvl.redis.connectionr    redisvl.utils.token_escaperr!   ulidr"   langgraph.store.redis.baser#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   token_unescaperr0   r+  rx  redis.asyncio.clusterr1   r   r3   r   r   r   <module>r     s   " " " " " "   * * * * * * 2 2 2 2 2 2 2 2 2 2       I I I I I I I I I I I I I I I I                          = < < < < < - - - - - - - - - - - - * * * * * * 2 2 2 2 2 2 2 2 ; ; ; ; ; ; 4 4 4 4 4 4                                  , + + + + +!>##  C C C C C CP- P- P- P- P-://02GP- P- P- P- P-r   